-
Notifications
You must be signed in to change notification settings - Fork 457
/
Copy pathScriptHostServiceLoggerExtension.cs
374 lines (309 loc) · 16.6 KB
/
ScriptHostServiceLoggerExtension.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Text.Json;
using Microsoft.Extensions.Logging;
namespace Microsoft.Azure.WebJobs.Script.WebHost.Diagnostics.Extensions
{
public static class ScriptHostServiceLoggerExtension
{
// EventId range is 500-599
private static readonly Action<ILogger, Exception> _scriptHostServiceInitCanceledByRuntime =
LoggerMessage.Define(
LogLevel.Information,
new EventId(500, nameof(ScriptHostServiceInitCanceledByRuntime)),
"Initialization cancellation requested by runtime.");
private static readonly Action<ILogger, int, TimeSpan, Exception> _unehealthyCountExceeded =
LoggerMessage.Define<int, TimeSpan>(
LogLevel.Error,
new EventId(501, nameof(UnhealthyCountExceeded)),
"Host unhealthy count exceeds the threshold of {healthCheckThreshold} for time window {healthCheckWindow}. Initiating shutdown.");
private static readonly Action<ILogger, Guid, Exception> _offline =
LoggerMessage.Define<Guid>(
LogLevel.Information,
new EventId(502, nameof(Offline)),
"Host created with operation id '{operationId}' is offline.");
private static readonly Action<ILogger, Guid, Exception> _initializing =
LoggerMessage.Define<Guid>(
LogLevel.Information,
new EventId(503, nameof(Initializing)),
"Initializing Host. OperationId: '{operationId}'.");
private static readonly Action<ILogger, int, int, Guid, Exception> _initialization =
LoggerMessage.Define<int, int, Guid>(
LogLevel.Information,
new EventId(504, nameof(Initialization)),
"Host initialization: ConsecutiveErrors={attemptCount}, StartupCount={startCount}, OperationId={operationId}");
private static readonly Action<ILogger, Guid, Exception> _inStandByMode =
LoggerMessage.Define<Guid>(
LogLevel.Information,
new EventId(505, nameof(InStandByMode)),
"Host is in standby mode. OperationId: '{operationId}'.");
private static readonly Action<ILogger, Exception> _unhealthyRestart =
LoggerMessage.Define(
LogLevel.Error,
new EventId(506, nameof(UnhealthyRestart)),
"Host is unhealthy. Initiating a restart.");
private static readonly Action<ILogger, Exception> _stopping =
LoggerMessage.Define(
LogLevel.Information,
new EventId(507, nameof(Stopping)),
"Stopping host...");
private static readonly Action<ILogger, Exception> _didNotShutDown =
LoggerMessage.Define(
LogLevel.Warning,
new EventId(508, nameof(DidNotShutDown)),
"Host did not shutdown within its allotted time.");
private static readonly Action<ILogger, Exception> _shutDownCompleted =
LoggerMessage.Define(
LogLevel.Information,
new EventId(509, nameof(ShutDownCompleted)),
"Host shutdown completed.");
private static readonly Action<ILogger, string, Exception> _skipRestart =
LoggerMessage.Define<string>(
LogLevel.Debug,
new EventId(510, nameof(SkipRestart)),
"Host restart was requested, but current host state is '{state}'. Skipping restart.");
private static readonly Action<ILogger, Exception> _restarting =
LoggerMessage.Define(
LogLevel.Information,
new EventId(511, nameof(Restarting)),
"Restarting host.");
private static readonly Action<ILogger, Exception> _restarted =
LoggerMessage.Define(
LogLevel.Information,
new EventId(512, nameof(Restarted)),
"Host restarted.");
private static readonly Action<ILogger, string, bool, bool, Guid, Exception> _building =
LoggerMessage.Define<string, bool, bool, Guid>(
LogLevel.Information,
new EventId(513, nameof(Building)),
"Building host: version spec: {functionsExtensionVersion}, startup suppressed: '{skipHostStartup}', configuration suppressed: '{skipHostJsonConfiguration}', startup operation id: '{operationId}'");
private static readonly Action<ILogger, Guid, Exception> _startupOperationWasCanceled =
LoggerMessage.Define<Guid>(
LogLevel.Debug,
new EventId(514, nameof(StartupOperationWasCanceled)),
"Host startup operation '{operationId}' was canceled.");
private static readonly Action<ILogger, Guid, Exception> _errorOccurredDuringStartupOperation =
LoggerMessage.Define<Guid>(
LogLevel.Error,
new EventId(515, nameof(ErrorOccurredDuringStartupOperation)),
"A host error has occurred during startup operation '{operationId}'.");
private static readonly Action<ILogger, Guid, Exception> _errorOccurredInactive =
LoggerMessage.Define<Guid>(
LogLevel.Warning,
new EventId(516, nameof(ErrorOccurredInactive)),
"A host error has occurred on an inactive host during startup operation '{operationId}'.");
private static readonly Action<ILogger, Guid, Exception> _cancellationRequested =
LoggerMessage.Define<Guid>(
LogLevel.Debug,
new EventId(517, nameof(CancellationRequested)),
"Cancellation requested for startup operation '{operationId}'. A new host will not be started.");
private static readonly Action<ILogger, string, string, Exception> _activeHostChanging =
LoggerMessage.Define<string, string>(
LogLevel.Debug,
new EventId(518, nameof(ActiveHostChanging)),
"Active host changing from '{oldHostInstanceId}' to '{newHostInstanceId}'.");
private static readonly Action<ILogger, Exception> _enteringRestart =
LoggerMessage.Define(
LogLevel.Debug,
new EventId(519, nameof(EnteringRestart)),
"Restart requested.");
private static readonly Action<ILogger, Exception> _restartBeforeStart =
LoggerMessage.Define(
LogLevel.Debug,
new EventId(520, nameof(RestartBeforeStart)),
"RestartAsync was called before StartAsync. Delaying restart until StartAsync has been called.");
private static readonly Action<ILogger, Guid, Exception> _startupOperationStarting =
LoggerMessage.Define<Guid>(
LogLevel.Debug,
new EventId(521, nameof(StartupOperationStarting)),
"Startup operation '{operationId}' starting.");
private static readonly Action<ILogger, Guid, Exception> _cancelingStartupOperationForRestart =
LoggerMessage.Define<Guid>(
LogLevel.Debug,
new EventId(522, nameof(CancelingStartupOperationForRestart)),
"Canceling startup operation '{operationId}' to unblock restart.");
// EventId 523 and 524 are defined in ScriptHostStartupOperation.
private static readonly Action<ILogger, Guid, string, Exception> _startupOperationStartingHost =
LoggerMessage.Define<Guid, string>(
LogLevel.Debug,
new EventId(525, nameof(StartupOperationStartingHost)),
"Startup operation '{operationId}' is starting host instance '{hostInstanceId}'.");
private static readonly Action<ILogger, Exception> _scriptHostServiceRestartCanceledByRuntime =
LoggerMessage.Define(
LogLevel.Information,
new EventId(526, nameof(ScriptHostServiceInitCanceledByRuntime)),
"Restart cancellation requested by runtime.");
private static readonly Action<ILogger, string, string, string, string, Exception> _executingHttpRequest =
LoggerMessage.Define<string, string, string, string>(
LogLevel.Information,
new EventId(527, nameof(ExecutingHttpRequest)),
Properties.Resources.ExecutingHttpRequest);
private static readonly Action<ILogger, string, string, int, long, Exception> _executedHttpRequest =
LoggerMessage.Define<string, string, int, long>(
LogLevel.Information,
new EventId(528, nameof(ExecutedHttpRequest)),
Properties.Resources.ExecutedHttpRequest);
private static readonly Action<ILogger, string, string, Exception> _hostStateChanged =
LoggerMessage.Define<string, string>(
LogLevel.Debug,
new EventId(529, nameof(HostStateChanged)),
"Host state changed from {previousState} to {newState}.");
private static readonly Action<ILogger, string, Exception> _logHostInitializationSettings =
LoggerMessage.Define<string>(
LogLevel.Debug,
new EventId(530, nameof(LogHostInitializationSettings)),
"{hostInitializationSettings}");
private static readonly Action<ILogger, string, Exception> _requestAborted =
LoggerMessage.Define<string>(
LogLevel.Debug,
new EventId(531, nameof(RequestAborted)),
"The request was aborted by the client (requestId: '{mS_ActivityId}').");
public static void HostStateChanged(this ILogger logger, ScriptHostState previousHostState, ScriptHostState newHostState)
{
var newState = newHostState.ToString();
var previousState = previousHostState.ToString();
_hostStateChanged(logger, previousState, newState, null);
}
public static void ExecutingHttpRequest(this ILogger logger, string mS_ActivityId, string httpMethod, string userAgent, string uri)
{
_executingHttpRequest(logger, mS_ActivityId, httpMethod, userAgent, uri, null);
}
public static void ExecutedHttpRequest(this ILogger logger, string mS_ActivityId, string identities, int statusCode, long duration)
{
_executedHttpRequest(logger, mS_ActivityId, identities, statusCode, duration, null);
}
public static void RequestAborted(this ILogger logger, string mS_ActivityId)
{
_requestAborted(logger, mS_ActivityId, null);
}
public static void ScriptHostServiceInitCanceledByRuntime(this ILogger logger)
{
_scriptHostServiceInitCanceledByRuntime(logger, null);
}
public static void UnhealthyCountExceeded(this ILogger logger, int healthCheckThreshold, TimeSpan healthCheckWindow)
{
_unehealthyCountExceeded(logger, healthCheckThreshold, healthCheckWindow, null);
}
public static void Offline(this ILogger logger, Guid operationId)
{
_offline(logger, operationId, null);
}
public static void Initializing(this ILogger logger, Guid operationId)
{
_initializing(logger, operationId, null);
}
public static void Initialization(this ILogger logger, int attemptCount, int startCount, Guid operationId)
{
_initialization(logger, attemptCount, startCount, operationId, null);
}
public static void InStandByMode(this ILogger logger, Guid operationId)
{
_inStandByMode(logger, operationId, null);
}
public static void UnhealthyRestart(this ILogger logger)
{
_unhealthyRestart(logger, null);
}
public static void Stopping(this ILogger logger)
{
_stopping(logger, null);
}
public static void DidNotShutDown(this ILogger logger)
{
_didNotShutDown(logger, null);
}
public static void ShutDownCompleted(this ILogger logger)
{
_shutDownCompleted(logger, null);
}
public static void SkipRestart(this ILogger logger, string state)
{
_skipRestart(logger, state, null);
}
public static void Restarting(this ILogger logger)
{
_restarting(logger, null);
}
public static void Restarted(this ILogger logger)
{
_restarted(logger, null);
}
public static void Building(this ILogger logger, string functionExtensionVersion, bool skipHostStartup, bool skipHostJsonConfiguration, Guid operationId)
{
_building(logger, functionExtensionVersion, skipHostStartup, skipHostJsonConfiguration, operationId, null);
}
public static void StartupOperationWasCanceled(this ILogger logger, Guid operationId)
{
_startupOperationWasCanceled(logger, operationId, null);
}
public static void ErrorOccurredDuringStartupOperation(this ILogger logger, Guid operationId, Exception ex)
{
_errorOccurredDuringStartupOperation(logger, operationId, ex);
}
public static void ErrorOccurredInactive(this ILogger logger, Guid operationId, Exception ex)
{
_errorOccurredInactive(logger, operationId, ex);
}
public static void CancellationRequested(this ILogger logger, Guid operationId)
{
_cancellationRequested(logger, operationId, null);
}
public static void ActiveHostChanging(this ILogger logger, string oldHostInstanceId, string newHostInstanceId)
{
_activeHostChanging(logger, oldHostInstanceId, newHostInstanceId, null);
}
public static void EnteringRestart(this ILogger logger)
{
_enteringRestart(logger, null);
}
public static void RestartBeforeStart(this ILogger logger)
{
_restartBeforeStart(logger, null);
}
public static void StartupOperationStarting(this ILogger logger, Guid operationId)
{
_startupOperationStarting(logger, operationId, null);
}
public static void CancelingStartupOperationForRestart(this ILogger logger, Guid operationId)
{
_cancelingStartupOperationForRestart(logger, operationId, null);
}
public static void StartupOperationStartingHost(this ILogger logger, Guid operationId, string hostInstanceId)
{
_startupOperationStartingHost(logger, operationId, hostInstanceId, null);
}
public static void ScriptHostServiceRestartCanceledByRuntime(this ILogger logger)
{
_scriptHostServiceRestartCanceledByRuntime(logger, null);
}
public static void LogHostInitializationSettings(this ILogger logger, string originalFunctionWorkerRuntime, string functionWorkerRuntime,
string originalFunctionWorkerRuntimeVersion, string functionsWorkerRuntimeVersion, string functionExtensionVersion, string hostDirectory,
bool inStandbyMode, bool hasBeenSpecialized, bool usePlaceholderDotNetIsolated, string websiteSku, string featureFlags,
IDictionary<string, string> hostingConfig, string hisMode)
{
// This is a dump of values for telemetry right now, but eventually we will refactor this
// into a proper Options object
var initializationSettings = new
{
OriginalFunctionWorkerRuntime = originalFunctionWorkerRuntime,
FunctionsWorkerRuntime = functionWorkerRuntime,
OriginalFunctionWorkerRuntimeVersion = originalFunctionWorkerRuntimeVersion,
FunctionsWorkerRuntimeVersion = functionsWorkerRuntimeVersion,
FunctionsExtensionVesion = functionExtensionVersion,
HostDirectory = hostDirectory,
InStandbyMode = inStandbyMode,
HasBeenSpecialized = hasBeenSpecialized,
UsePlaceholderDotNetIsolated = usePlaceholderDotNetIsolated,
WebSiteSku = websiteSku,
FeatureFlags = featureFlags,
HostingConfig = hostingConfig,
HISMode = hisMode
};
var options = new JsonSerializerOptions { WriteIndented = true };
string settingsJson = JsonSerializer.Serialize(initializationSettings, options);
_logHostInitializationSettings(logger, settingsJson, null);
}
}
}