33
44using System ;
55using System . Collections . Generic ;
6+ using System . Diagnostics ;
67using Microsoft . Azure . WebJobs . Logging ;
8+ using Microsoft . Azure . WebJobs . Script . Configuration ;
79using Microsoft . Azure . WebJobs . Script . WebHost ;
810using Microsoft . Azure . WebJobs . Script . WebHost . Diagnostics ;
911using Microsoft . Extensions . Logging ;
@@ -20,11 +22,14 @@ public class DiagnosticLoggerTests
2022 private static readonly string _regionName = "West US" ;
2123 private static readonly string _websiteHostName = "functionstest.azurewebsites.net" ;
2224 private static readonly string _subscriptionId = "e3235165-1600-4819-85f0-2ab362e909e4" ;
25+ private static readonly string _roleInstance = "e777fde04dea4eb931d5e5f06e65b4fdf5b375aed60af41dd7b491cf5792e01b" ;
26+ private static readonly int _processId = Process . GetCurrentProcess ( ) . Id ;
2327 private readonly string _hostInstanceId = Guid . NewGuid ( ) . ToString ( ) ;
2428
2529 private readonly AzureMonitorDiagnosticLogger _logger ;
2630 private readonly Mock < IEventGenerator > _mockEventGenerator ;
2731 private readonly IEnvironment _environment = new TestEnvironment ( ) ;
32+ private readonly TestOptionsMonitor < AppServiceOptions > _appServiceOptionsWrapper ;
2833 private readonly string _category = LogCategories . CreateFunctionCategory ( _functionName ) ;
2934 private readonly HostNameProvider _hostNameProvider ;
3035
@@ -33,15 +38,26 @@ public DiagnosticLoggerTests()
3338 _environment . SetEnvironmentVariable ( EnvironmentSettingNames . AzureWebsiteOwnerName , $ "{ _subscriptionId } +westuswebspace") ;
3439 _environment . SetEnvironmentVariable ( EnvironmentSettingNames . AzureWebsiteHostName , _websiteHostName ) ;
3540 _environment . SetEnvironmentVariable ( EnvironmentSettingNames . RegionName , _regionName ) ;
41+ _environment . SetEnvironmentVariable ( EnvironmentSettingNames . AzureWebsiteInstanceId , _roleInstance ) ;
3642
3743 _mockEventGenerator = new Mock < IEventGenerator > ( MockBehavior . Strict ) ;
3844
45+ var appServiceOptions = new AppServiceOptions
46+ {
47+ AppName = "TestApp" ,
48+ SlotName = "Production" ,
49+ SubscriptionId = "abc123" ,
50+ RuntimeSiteName = "TestApp_Runtime"
51+ } ;
52+
53+ _appServiceOptionsWrapper = new TestOptionsMonitor < AppServiceOptions > ( appServiceOptions ) ;
54+
3955 _category = LogCategories . CreateFunctionCategory ( _functionName ) ;
4056 var loggerProvider = new TestLoggerProvider ( ) ;
4157 var loggerFactory = new LoggerFactory ( ) ;
4258 loggerFactory . AddProvider ( loggerProvider ) ;
4359 _hostNameProvider = new HostNameProvider ( _environment ) ;
44- _logger = new AzureMonitorDiagnosticLogger ( _category , _hostInstanceId , _mockEventGenerator . Object , _environment , new LoggerExternalScopeProvider ( ) , _hostNameProvider ) ;
60+ _logger = new AzureMonitorDiagnosticLogger ( _category , _hostInstanceId , _mockEventGenerator . Object , _environment , new LoggerExternalScopeProvider ( ) , _hostNameProvider , _appServiceOptionsWrapper ) ;
4561 }
4662
4763 [ Fact ]
@@ -61,22 +77,30 @@ public void Log_EmitsExpectedEvent()
6177
6278 using ( CreateScope ( activityId : activityId , functionName : _functionName , functionInvocationId : functionInvocationId ) )
6379 {
64- _logger . LogDebug ( message ) ;
80+ _logger . LogDebug ( new EventId ( 123 , "TestEvent" ) , message ) ;
6581 }
6682
6783 _mockEventGenerator . VerifyAll ( ) ;
6884
6985 JObject actual = JObject . Parse ( properties ) ;
86+
87+ var level = LogLevel . Debug ;
7088 JObject expected = JObject . FromObject ( new
7189 {
90+ appName = _appServiceOptionsWrapper . CurrentValue . AppName ,
91+ roleInstance = _roleInstance ,
7292 message ,
7393 category = _category ,
7494 hostVersion = ScriptHost . Version ,
7595 functionInvocationId ,
7696 functionName = _functionName ,
7797 hostInstanceId = _hostInstanceId ,
7898 activityId ,
79- level = "Debug"
99+ level = level . ToString ( ) ,
100+ levelId = ( int ) level ,
101+ processId = _processId ,
102+ eventId = 123 ,
103+ eventName = "TestEvent"
80104 } ) ;
81105
82106 Assert . True ( JToken . DeepEquals ( actual , expected ) , $ "Actual: { actual . ToString ( ) } { Environment . NewLine } Expected: { expected . ToString ( ) } ") ;
@@ -107,17 +131,23 @@ public void Log_Error_EmitsExpectedEvent()
107131
108132 _mockEventGenerator . VerifyAll ( ) ;
109133
134+ var level = LogLevel . Error ;
135+
110136 JObject actual = JObject . Parse ( properties ) ;
111137 JObject expected = JObject . FromObject ( new
112138 {
139+ appName = _appServiceOptionsWrapper . CurrentValue . AppName ,
140+ roleInstance = _roleInstance ,
113141 exceptionType = ex . GetType ( ) . ToString ( ) ,
114142 exceptionMessage = ex . Message ,
115143 exceptionDetails = ex . ToFormattedString ( ) ,
116144 message ,
117145 category = _category ,
118146 hostInstanceId = _hostInstanceId ,
119147 hostVersion = ScriptHost . Version ,
120- level = "Error"
148+ level = level . ToString ( ) ,
149+ levelId = ( int ) level ,
150+ processId = _processId
121151 } ) ;
122152
123153 Assert . True ( JToken . DeepEquals ( actual , expected ) , $ "Actual: { actual . ToString ( ) } { Environment . NewLine } Expected: { expected . ToString ( ) } ") ;
@@ -152,9 +182,13 @@ public void Log_Sanitizes()
152182
153183 _mockEventGenerator . VerifyAll ( ) ;
154184
185+ var level = LogLevel . Error ;
186+
155187 JObject actual = JObject . Parse ( properties ) ;
156188 JObject expected = JObject . FromObject ( new
157189 {
190+ appName = _appServiceOptionsWrapper . CurrentValue . AppName ,
191+ roleInstance = _roleInstance ,
158192 category = _category ,
159193 exceptionDetails = sanitizedDetails ,
160194 exceptionMessage = sanitizedExceptionMessage ,
@@ -163,8 +197,10 @@ public void Log_Sanitizes()
163197 functionName = _functionName ,
164198 hostInstanceId = _hostInstanceId ,
165199 hostVersion = ScriptHost . Version ,
166- level = "Error" ,
200+ level = level . ToString ( ) ,
201+ levelId = ( int ) level ,
167202 message = sanitizedString ,
203+ processId = _processId
168204 } ) ;
169205
170206 Assert . True ( JToken . DeepEquals ( actual , expected ) , $ "Actual: { actual . ToString ( ) } { Environment . NewLine } Expected: { expected . ToString ( ) } ") ;
@@ -194,7 +230,7 @@ public void Log_DisabledIfNoSiteName()
194230 _environment . SetEnvironmentVariable ( EnvironmentSettingNames . AzureWebsiteHostName , null ) ;
195231
196232 // Recreate the logger was we cache the site name in the constructor
197- ILogger logger = new AzureMonitorDiagnosticLogger ( _category , _hostInstanceId , _mockEventGenerator . Object , _environment , new LoggerExternalScopeProvider ( ) , _hostNameProvider ) ;
233+ ILogger logger = new AzureMonitorDiagnosticLogger ( _category , _hostInstanceId , _mockEventGenerator . Object , _environment , new LoggerExternalScopeProvider ( ) , _hostNameProvider , _appServiceOptionsWrapper ) ;
198234
199235 logger . LogInformation ( message ) ;
200236
0 commit comments