3
3
4
4
using System ;
5
5
using System . Collections . Generic ;
6
+ using System . Diagnostics ;
6
7
using Microsoft . Azure . WebJobs . Logging ;
8
+ using Microsoft . Azure . WebJobs . Script . Configuration ;
7
9
using Microsoft . Azure . WebJobs . Script . WebHost ;
8
10
using Microsoft . Azure . WebJobs . Script . WebHost . Diagnostics ;
9
11
using Microsoft . Extensions . Logging ;
@@ -20,11 +22,14 @@ public class DiagnosticLoggerTests
20
22
private static readonly string _regionName = "West US" ;
21
23
private static readonly string _websiteHostName = "functionstest.azurewebsites.net" ;
22
24
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 ;
23
27
private readonly string _hostInstanceId = Guid . NewGuid ( ) . ToString ( ) ;
24
28
25
29
private readonly AzureMonitorDiagnosticLogger _logger ;
26
30
private readonly Mock < IEventGenerator > _mockEventGenerator ;
27
31
private readonly IEnvironment _environment = new TestEnvironment ( ) ;
32
+ private readonly TestOptionsMonitor < AppServiceOptions > _appServiceOptionsWrapper ;
28
33
private readonly string _category = LogCategories . CreateFunctionCategory ( _functionName ) ;
29
34
private readonly HostNameProvider _hostNameProvider ;
30
35
@@ -33,15 +38,26 @@ public DiagnosticLoggerTests()
33
38
_environment . SetEnvironmentVariable ( EnvironmentSettingNames . AzureWebsiteOwnerName , $ "{ _subscriptionId } +westuswebspace") ;
34
39
_environment . SetEnvironmentVariable ( EnvironmentSettingNames . AzureWebsiteHostName , _websiteHostName ) ;
35
40
_environment . SetEnvironmentVariable ( EnvironmentSettingNames . RegionName , _regionName ) ;
41
+ _environment . SetEnvironmentVariable ( EnvironmentSettingNames . AzureWebsiteInstanceId , _roleInstance ) ;
36
42
37
43
_mockEventGenerator = new Mock < IEventGenerator > ( MockBehavior . Strict ) ;
38
44
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
+
39
55
_category = LogCategories . CreateFunctionCategory ( _functionName ) ;
40
56
var loggerProvider = new TestLoggerProvider ( ) ;
41
57
var loggerFactory = new LoggerFactory ( ) ;
42
58
loggerFactory . AddProvider ( loggerProvider ) ;
43
59
_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 ) ;
45
61
}
46
62
47
63
[ Fact ]
@@ -61,22 +77,30 @@ public void Log_EmitsExpectedEvent()
61
77
62
78
using ( CreateScope ( activityId : activityId , functionName : _functionName , functionInvocationId : functionInvocationId ) )
63
79
{
64
- _logger . LogDebug ( message ) ;
80
+ _logger . LogDebug ( new EventId ( 123 , "TestEvent" ) , message ) ;
65
81
}
66
82
67
83
_mockEventGenerator . VerifyAll ( ) ;
68
84
69
85
JObject actual = JObject . Parse ( properties ) ;
86
+
87
+ var level = LogLevel . Debug ;
70
88
JObject expected = JObject . FromObject ( new
71
89
{
90
+ appName = _appServiceOptionsWrapper . CurrentValue . AppName ,
91
+ roleInstance = _roleInstance ,
72
92
message ,
73
93
category = _category ,
74
94
hostVersion = ScriptHost . Version ,
75
95
functionInvocationId ,
76
96
functionName = _functionName ,
77
97
hostInstanceId = _hostInstanceId ,
78
98
activityId ,
79
- level = "Debug"
99
+ level = level . ToString ( ) ,
100
+ levelId = ( int ) level ,
101
+ processId = _processId ,
102
+ eventId = 123 ,
103
+ eventName = "TestEvent"
80
104
} ) ;
81
105
82
106
Assert . True ( JToken . DeepEquals ( actual , expected ) , $ "Actual: { actual . ToString ( ) } { Environment . NewLine } Expected: { expected . ToString ( ) } ") ;
@@ -107,17 +131,23 @@ public void Log_Error_EmitsExpectedEvent()
107
131
108
132
_mockEventGenerator . VerifyAll ( ) ;
109
133
134
+ var level = LogLevel . Error ;
135
+
110
136
JObject actual = JObject . Parse ( properties ) ;
111
137
JObject expected = JObject . FromObject ( new
112
138
{
139
+ appName = _appServiceOptionsWrapper . CurrentValue . AppName ,
140
+ roleInstance = _roleInstance ,
113
141
exceptionType = ex . GetType ( ) . ToString ( ) ,
114
142
exceptionMessage = ex . Message ,
115
143
exceptionDetails = ex . ToFormattedString ( ) ,
116
144
message ,
117
145
category = _category ,
118
146
hostInstanceId = _hostInstanceId ,
119
147
hostVersion = ScriptHost . Version ,
120
- level = "Error"
148
+ level = level . ToString ( ) ,
149
+ levelId = ( int ) level ,
150
+ processId = _processId
121
151
} ) ;
122
152
123
153
Assert . True ( JToken . DeepEquals ( actual , expected ) , $ "Actual: { actual . ToString ( ) } { Environment . NewLine } Expected: { expected . ToString ( ) } ") ;
@@ -152,9 +182,13 @@ public void Log_Sanitizes()
152
182
153
183
_mockEventGenerator . VerifyAll ( ) ;
154
184
185
+ var level = LogLevel . Error ;
186
+
155
187
JObject actual = JObject . Parse ( properties ) ;
156
188
JObject expected = JObject . FromObject ( new
157
189
{
190
+ appName = _appServiceOptionsWrapper . CurrentValue . AppName ,
191
+ roleInstance = _roleInstance ,
158
192
category = _category ,
159
193
exceptionDetails = sanitizedDetails ,
160
194
exceptionMessage = sanitizedExceptionMessage ,
@@ -163,8 +197,10 @@ public void Log_Sanitizes()
163
197
functionName = _functionName ,
164
198
hostInstanceId = _hostInstanceId ,
165
199
hostVersion = ScriptHost . Version ,
166
- level = "Error" ,
200
+ level = level . ToString ( ) ,
201
+ levelId = ( int ) level ,
167
202
message = sanitizedString ,
203
+ processId = _processId
168
204
} ) ;
169
205
170
206
Assert . True ( JToken . DeepEquals ( actual , expected ) , $ "Actual: { actual . ToString ( ) } { Environment . NewLine } Expected: { expected . ToString ( ) } ") ;
@@ -194,7 +230,7 @@ public void Log_DisabledIfNoSiteName()
194
230
_environment . SetEnvironmentVariable ( EnvironmentSettingNames . AzureWebsiteHostName , null ) ;
195
231
196
232
// 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 ) ;
198
234
199
235
logger . LogInformation ( message ) ;
200
236
0 commit comments