@@ -1266,109 +1266,128 @@ public void ConfigureLoggerFactory_Default()
1266
1266
[ Fact ]
1267
1267
public void ConfigureLoggerFactory_ApplicationInsights ( )
1268
1268
{
1269
- var config = new ScriptHostConfiguration ( ) ;
1270
- var mockTraceFactory = new Mock < IFunctionTraceWriterFactory > ( MockBehavior . Strict ) ;
1271
- var loggerFactory = new TestLoggerFactory ( ) ;
1272
- config . HostConfig . LoggerFactory = loggerFactory ;
1269
+ try
1270
+ {
1271
+ var config = new ScriptHostConfiguration ( ) ;
1272
+ var mockTraceFactory = new Mock < IFunctionTraceWriterFactory > ( MockBehavior . Strict ) ;
1273
+ var loggerFactory = new TestLoggerFactory ( ) ;
1274
+ config . HostConfig . LoggerFactory = loggerFactory ;
1273
1275
1274
- // Make sure no App Insights is configured
1275
- var settingsManager = ScriptSettingsManager . Instance ;
1276
- settingsManager . ApplicationInsightsInstrumentationKey = "Some_Instrumentation_Key" ;
1276
+ // configure via AppSettings, because the test project has configured an
1277
+ // empty value in app.config which we need to override
1278
+ var settingsManager = ScriptSettingsManager . Instance ;
1279
+ ConfigurationManager . AppSettings [ EnvironmentSettingNames . AppInsightsInstrumentationKey ] = "Some_Instrumentation_Key" ;
1277
1280
1278
- var metricsLogger = new TestMetricsLogger ( ) ;
1279
- config . HostConfig . AddService < IMetricsLogger > ( metricsLogger ) ;
1281
+ var metricsLogger = new TestMetricsLogger ( ) ;
1282
+ config . HostConfig . AddService < IMetricsLogger > ( metricsLogger ) ;
1280
1283
1281
- ScriptHost . ConfigureLoggerFactory ( config , mockTraceFactory . Object , settingsManager , ( ) => true ) ;
1284
+ ScriptHost . ConfigureLoggerFactory ( config , mockTraceFactory . Object , settingsManager , ( ) => true ) ;
1282
1285
1283
- Assert . Equal ( 2 , loggerFactory . Providers . Count ) ;
1286
+ Assert . Equal ( 2 , loggerFactory . Providers . Count ) ;
1284
1287
1285
- Assert . Equal ( 1 , loggerFactory . Providers . OfType < FileLoggerProvider > ( ) . Count ( ) ) ;
1288
+ Assert . Equal ( 1 , loggerFactory . Providers . OfType < FileLoggerProvider > ( ) . Count ( ) ) ;
1286
1289
1287
- // The app insights logger is internal, so just check the name
1288
- ILoggerProvider appInsightsProvider = loggerFactory . Providers . Last ( ) ;
1289
- Assert . Equal ( "ApplicationInsightsLoggerProvider" , appInsightsProvider . GetType ( ) . Name ) ;
1290
+ // The app insights logger is internal, so just check the name
1291
+ ILoggerProvider appInsightsProvider = loggerFactory . Providers . Last ( ) ;
1292
+ Assert . Equal ( "ApplicationInsightsLoggerProvider" , appInsightsProvider . GetType ( ) . Name ) ;
1290
1293
1291
- Assert . Equal ( 1 , metricsLogger . LoggedEvents . Count ) ;
1292
- Assert . Equal ( MetricEventNames . ApplicationInsightsEnabled , metricsLogger . LoggedEvents [ 0 ] ) ;
1294
+ Assert . Equal ( 1 , metricsLogger . LoggedEvents . Count ) ;
1295
+ Assert . Equal ( MetricEventNames . ApplicationInsightsEnabled , metricsLogger . LoggedEvents [ 0 ] ) ;
1296
+ }
1297
+ finally
1298
+ {
1299
+ // restore the empty value configured for tests in app.config
1300
+ ConfigurationManager . AppSettings [ EnvironmentSettingNames . AppInsightsInstrumentationKey ] = string . Empty ;
1301
+ }
1293
1302
}
1294
1303
1295
1304
[ Fact ]
1296
1305
public void DefaultLoggerFactory_BeginScope ( )
1297
1306
{
1298
- var trace = new TestTraceWriter ( TraceLevel . Info ) ;
1299
- var config = new ScriptHostConfiguration ( ) ;
1300
- var mockTraceFactory = new Mock < IFunctionTraceWriterFactory > ( MockBehavior . Strict ) ;
1301
- mockTraceFactory
1302
- . Setup ( f => f . Create ( "Test" , null ) )
1303
- . Returns ( trace ) ;
1304
-
1305
- var channel = new TestTelemetryChannel ( ) ;
1306
- var builder = new TestChannelLoggerFactoryBuilder ( channel ) ;
1307
+ try
1308
+ {
1309
+ var trace = new TestTraceWriter ( TraceLevel . Info ) ;
1310
+ var config = new ScriptHostConfiguration ( ) ;
1311
+ var mockTraceFactory = new Mock < IFunctionTraceWriterFactory > ( MockBehavior . Strict ) ;
1312
+ mockTraceFactory
1313
+ . Setup ( f => f . Create ( "Test" , null ) )
1314
+ . Returns ( trace ) ;
1307
1315
1308
- config . LoggerFactoryBuilder = builder ;
1309
- config . HostConfig . LoggerFactory = new LoggerFactory ( ) ;
1316
+ var channel = new TestTelemetryChannel ( ) ;
1317
+ var builder = new TestChannelLoggerFactoryBuilder ( channel ) ;
1310
1318
1311
- var settingsManager = ScriptSettingsManager . Instance ;
1312
- settingsManager . ApplicationInsightsInstrumentationKey = TestChannelLoggerFactoryBuilder . ApplicationInsightsKey ;
1319
+ config . LoggerFactoryBuilder = builder ;
1320
+ config . HostConfig . LoggerFactory = new LoggerFactory ( ) ;
1313
1321
1314
- ScriptHost . ConfigureLoggerFactory ( config , mockTraceFactory . Object , settingsManager , ( ) => true ) ;
1322
+ // configure via AppSettings, because the test project has configured an
1323
+ // empty value in app.config which we need to override
1324
+ var settingsManager = ScriptSettingsManager . Instance ;
1325
+ ConfigurationManager . AppSettings [ EnvironmentSettingNames . AppInsightsInstrumentationKey ] = TestChannelLoggerFactoryBuilder . ApplicationInsightsKey ;
1315
1326
1316
- // Create a logger and try out the configured factory. We need to pretend that it is coming from a
1317
- // function, so set the function name and the category appropriately.
1318
- var logger = config . HostConfig . LoggerFactory . CreateLogger ( LogCategories . Function ) ;
1327
+ ScriptHost . ConfigureLoggerFactory ( config , mockTraceFactory . Object , settingsManager , ( ) => true ) ;
1319
1328
1320
- using ( logger . BeginScope ( new Dictionary < string , object >
1321
- {
1322
- [ ScriptConstants . LoggerFunctionNameKey ] = "Test"
1323
- } ) )
1324
- {
1325
- // Now log as if from within a function.
1329
+ // Create a logger and try out the configured factory. We need to pretend that it is coming from a
1330
+ // function, so set the function name and the category appropriately.
1331
+ var logger = config . HostConfig . LoggerFactory . CreateLogger ( LogCategories . Function ) ;
1326
1332
1327
- // Test that both dictionaries and structured logs work as state
1328
- // and that nesting works as expected.
1329
- using ( logger . BeginScope ( "{customKey1}" , "customValue1" ) )
1333
+ using ( logger . BeginScope ( new Dictionary < string , object >
1330
1334
{
1331
- logger . LogInformation ( "1" ) ;
1335
+ [ ScriptConstants . LoggerFunctionNameKey ] = "Test"
1336
+ } ) )
1337
+ {
1338
+ // Now log as if from within a function.
1332
1339
1333
- using ( logger . BeginScope ( new Dictionary < string , object >
1334
- {
1335
- [ "customKey2" ] = "customValue2"
1336
- } ) )
1340
+ // Test that both dictionaries and structured logs work as state
1341
+ // and that nesting works as expected.
1342
+ using ( logger . BeginScope ( "{customKey1}" , "customValue1" ) )
1337
1343
{
1338
- logger . LogInformation ( "2" ) ;
1339
- }
1344
+ logger . LogInformation ( "1" ) ;
1345
+
1346
+ using ( logger . BeginScope ( new Dictionary < string , object >
1347
+ {
1348
+ [ "customKey2" ] = "customValue2"
1349
+ } ) )
1350
+ {
1351
+ logger . LogInformation ( "2" ) ;
1352
+ }
1340
1353
1341
- logger . LogInformation ( "3" ) ;
1342
- }
1354
+ logger . LogInformation ( "3" ) ;
1355
+ }
1343
1356
1344
- using ( logger . BeginScope ( "should not throw" ) )
1345
- {
1346
- logger . LogInformation ( "4" ) ;
1357
+ using ( logger . BeginScope ( "should not throw" ) )
1358
+ {
1359
+ logger . LogInformation ( "4" ) ;
1360
+ }
1347
1361
}
1348
- }
1349
1362
1350
- Assert . Equal ( 4 , trace . GetTraces ( ) . Count ) ;
1351
- Assert . Equal ( 4 , channel . Telemetries . Count ) ;
1363
+ Assert . Equal ( 4 , trace . GetTraces ( ) . Count ) ;
1364
+ Assert . Equal ( 4 , channel . Telemetries . Count ) ;
1352
1365
1353
- var traces = channel . Telemetries . Cast < TraceTelemetry > ( ) . OrderBy ( t => t . Message ) . ToArray ( ) ;
1366
+ var traces = channel . Telemetries . Cast < TraceTelemetry > ( ) . OrderBy ( t => t . Message ) . ToArray ( ) ;
1354
1367
1355
- // Every telemetry will have {originalFormat}, Category, Level, but we validate those elsewhere.
1356
- // We're only interested in the custom properties.
1357
- Assert . Equal ( "1" , traces [ 0 ] . Message ) ;
1358
- Assert . Equal ( 4 , traces [ 0 ] . Properties . Count ) ;
1359
- Assert . Equal ( "customValue1" , traces [ 0 ] . Properties [ "prop__customKey1" ] ) ;
1368
+ // Every telemetry will have {originalFormat}, Category, Level, but we validate those elsewhere.
1369
+ // We're only interested in the custom properties.
1370
+ Assert . Equal ( "1" , traces [ 0 ] . Message ) ;
1371
+ Assert . Equal ( 4 , traces [ 0 ] . Properties . Count ) ;
1372
+ Assert . Equal ( "customValue1" , traces [ 0 ] . Properties [ "prop__customKey1" ] ) ;
1360
1373
1361
- Assert . Equal ( "2" , traces [ 1 ] . Message ) ;
1362
- Assert . Equal ( 5 , traces [ 1 ] . Properties . Count ) ;
1363
- Assert . Equal ( "customValue1" , traces [ 1 ] . Properties [ "prop__customKey1" ] ) ;
1364
- Assert . Equal ( "customValue2" , traces [ 1 ] . Properties [ "prop__customKey2" ] ) ;
1374
+ Assert . Equal ( "2" , traces [ 1 ] . Message ) ;
1375
+ Assert . Equal ( 5 , traces [ 1 ] . Properties . Count ) ;
1376
+ Assert . Equal ( "customValue1" , traces [ 1 ] . Properties [ "prop__customKey1" ] ) ;
1377
+ Assert . Equal ( "customValue2" , traces [ 1 ] . Properties [ "prop__customKey2" ] ) ;
1365
1378
1366
- Assert . Equal ( "3" , traces [ 2 ] . Message ) ;
1367
- Assert . Equal ( 4 , traces [ 2 ] . Properties . Count ) ;
1368
- Assert . Equal ( "customValue1" , traces [ 2 ] . Properties [ "prop__customKey1" ] ) ;
1379
+ Assert . Equal ( "3" , traces [ 2 ] . Message ) ;
1380
+ Assert . Equal ( 4 , traces [ 2 ] . Properties . Count ) ;
1381
+ Assert . Equal ( "customValue1" , traces [ 2 ] . Properties [ "prop__customKey1" ] ) ;
1369
1382
1370
- Assert . Equal ( "4" , traces [ 3 ] . Message ) ;
1371
- Assert . Equal ( 3 , traces [ 3 ] . Properties . Count ) ;
1383
+ Assert . Equal ( "4" , traces [ 3 ] . Message ) ;
1384
+ Assert . Equal ( 3 , traces [ 3 ] . Properties . Count ) ;
1385
+ }
1386
+ finally
1387
+ {
1388
+ // restore the empty value configured for tests in app.config
1389
+ ConfigurationManager . AppSettings [ EnvironmentSettingNames . AppInsightsInstrumentationKey ] = string . Empty ;
1390
+ }
1372
1391
}
1373
1392
1374
1393
[ Fact ]
0 commit comments