@@ -46,7 +46,7 @@ public override async Task<HealthReport> CheckHealthAsync(
46
46
}
47
47
48
48
var totalTime = ValueStopwatch . StartNew ( ) ;
49
- HealthCheckProcessingBegin ( ) ;
49
+ Log . HealthCheckProcessingBegin ( _logger ) ;
50
50
51
51
var tasks = new Task < HealthReportEntry > [ registrations . Count ] ;
52
52
var index = 0 ;
@@ -68,7 +68,7 @@ public override async Task<HealthReport> CheckHealthAsync(
68
68
69
69
var totalElapsedTime = totalTime . GetElapsedTime ( ) ;
70
70
var report = new HealthReport ( entries , totalElapsedTime ) ;
71
- HealthCheckProcessingEnd ( report . Status , totalElapsedTime ) ;
71
+ Log . HealthCheckProcessingEnd ( _logger , report . Status , totalElapsedTime ) ;
72
72
return report ;
73
73
}
74
74
@@ -87,7 +87,7 @@ private async Task<HealthReportEntry> RunCheckAsync(HealthCheckRegistration regi
87
87
var stopwatch = ValueStopwatch . StartNew ( ) ;
88
88
var context = new HealthCheckContext { Registration = registration } ;
89
89
90
- HealthCheckBegin ( registration . Name ) ;
90
+ Log . HealthCheckBegin ( _logger , registration . Name ) ;
91
91
92
92
HealthReportEntry entry ;
93
93
CancellationTokenSource ? timeoutCancellationTokenSource = null ;
@@ -115,8 +115,8 @@ private async Task<HealthReportEntry> RunCheckAsync(HealthCheckRegistration regi
115
115
data : result . Data ,
116
116
tags : registration . Tags ) ;
117
117
118
- HealthCheckEnd ( registration , entry , duration ) ;
119
- HealthCheckData ( registration , entry ) ;
118
+ Log . HealthCheckEnd ( _logger , registration , entry , duration ) ;
119
+ Log . HealthCheckData ( _logger , registration , entry ) ;
120
120
}
121
121
catch ( OperationCanceledException ex ) when ( ! cancellationToken . IsCancellationRequested )
122
122
{
@@ -129,7 +129,7 @@ private async Task<HealthReportEntry> RunCheckAsync(HealthCheckRegistration regi
129
129
data : null ,
130
130
tags : registration . Tags ) ;
131
131
132
- HealthCheckError ( registration , ex , duration ) ;
132
+ Log . HealthCheckError ( _logger , registration , ex , duration ) ;
133
133
}
134
134
135
135
// Allow cancellation to propagate if it's not a timeout.
@@ -144,7 +144,7 @@ private async Task<HealthReportEntry> RunCheckAsync(HealthCheckRegistration regi
144
144
data : null ,
145
145
tags : registration . Tags ) ;
146
146
147
- HealthCheckError ( registration , ex , duration ) ;
147
+ Log . HealthCheckError ( _logger , registration , ex , duration ) ;
148
148
}
149
149
150
150
finally
@@ -189,75 +189,85 @@ internal static class EventIds
189
189
public const int HealthCheckErrorId = 104 ;
190
190
public const int HealthCheckDataId = 105 ;
191
191
192
- public static readonly EventId HealthCheckProcessingBegin = new EventId ( HealthCheckProcessingBeginId , nameof ( HealthCheckProcessingBegin ) ) ;
193
- public static readonly EventId HealthCheckProcessingEnd = new EventId ( HealthCheckProcessingEndId , nameof ( HealthCheckProcessingEnd ) ) ;
194
-
195
- public static readonly EventId HealthCheckBegin = new EventId ( HealthCheckBeginId , nameof ( HealthCheckBegin ) ) ;
196
- public static readonly EventId HealthCheckEnd = new EventId ( HealthCheckEndId , nameof ( HealthCheckEnd ) ) ;
197
- public static readonly EventId HealthCheckError = new EventId ( HealthCheckErrorId , nameof ( HealthCheckError ) ) ;
198
- public static readonly EventId HealthCheckData = new EventId ( HealthCheckDataId , nameof ( HealthCheckData ) ) ;
192
+ // Hard code the event names to avoid breaking changes. Even if the methods are renamed, these hard-coded names shouldn't change.
193
+ public const string HealthCheckProcessingBeginName = "HealthCheckProcessingBegin" ;
194
+ public const string HealthCheckProcessingEndName = "HealthCheckProcessingEnd" ;
195
+ public const string HealthCheckBeginName = "HealthCheckBegin" ;
196
+ public const string HealthCheckEndName = "HealthCheckEnd" ;
197
+ public const string HealthCheckErrorName = "HealthCheckError" ;
198
+ public const string HealthCheckDataName = "HealthCheckData" ;
199
+
200
+ public static readonly EventId HealthCheckProcessingBegin = new EventId ( HealthCheckProcessingBeginId , HealthCheckProcessingBeginName ) ;
201
+ public static readonly EventId HealthCheckProcessingEnd = new EventId ( HealthCheckProcessingEndId , HealthCheckProcessingEndName ) ;
202
+ public static readonly EventId HealthCheckBegin = new EventId ( HealthCheckBeginId , HealthCheckBeginName ) ;
203
+ public static readonly EventId HealthCheckEnd = new EventId ( HealthCheckEndId , HealthCheckEndName ) ;
204
+ public static readonly EventId HealthCheckError = new EventId ( HealthCheckErrorId , HealthCheckErrorName ) ;
205
+ public static readonly EventId HealthCheckData = new EventId ( HealthCheckDataId , HealthCheckDataName ) ;
199
206
}
200
207
201
- [ LoggerMessage ( EventId = EventIds . HealthCheckProcessingBeginId , Level = LogLevel . Debug , Message = "Running health checks" ) ]
202
- private partial void HealthCheckProcessingBegin ( ) ;
208
+ private static partial class Log
209
+ {
210
+ [ LoggerMessage ( EventIds . HealthCheckProcessingBeginId , LogLevel . Debug , "Running health checks" , EventName = EventIds . HealthCheckProcessingBeginName ) ]
211
+ public static partial void HealthCheckProcessingBegin ( ILogger logger ) ;
203
212
204
- private void HealthCheckProcessingEnd ( HealthStatus status , TimeSpan duration ) =>
205
- HealthCheckProcessingEnd ( status , duration . TotalMilliseconds ) ;
213
+ public static void HealthCheckProcessingEnd ( ILogger logger , HealthStatus status , TimeSpan duration ) =>
214
+ HealthCheckProcessingEnd ( logger , status , duration . TotalMilliseconds ) ;
206
215
207
- [ LoggerMessage ( EventId = EventIds . HealthCheckProcessingEndId , Level = LogLevel . Debug , Message = "Health check processing with combined status {HealthStatus} completed after {ElapsedMilliseconds}ms" ) ]
208
- private partial void HealthCheckProcessingEnd ( HealthStatus HealthStatus , double ElapsedMilliseconds ) ;
216
+ [ LoggerMessage ( EventIds . HealthCheckProcessingEndId , LogLevel . Debug , "Health check processing with combined status {HealthStatus} completed after {ElapsedMilliseconds}ms" , EventName = EventIds . HealthCheckProcessingEndName ) ]
217
+ private static partial void HealthCheckProcessingEnd ( ILogger logger , HealthStatus HealthStatus , double ElapsedMilliseconds ) ;
209
218
210
- [ LoggerMessage ( EventId = EventIds . HealthCheckBeginId , Level = LogLevel . Debug , Message = "Running health check {HealthCheckName}" ) ]
211
- private partial void HealthCheckBegin ( string HealthCheckName ) ;
219
+ [ LoggerMessage ( EventIds . HealthCheckBeginId , LogLevel . Debug , "Running health check {HealthCheckName}" , EventName = EventIds . HealthCheckBeginName ) ]
220
+ public static partial void HealthCheckBegin ( ILogger logger , string HealthCheckName ) ;
212
221
213
- // These are separate so they can have different log levels
214
- private const string HealthCheckEndText = "Health check {HealthCheckName} with status {HealthStatus} completed after {ElapsedMilliseconds}ms with message '{HealthCheckDescription}'" ;
222
+ // These are separate so they can have different log levels
223
+ private const string HealthCheckEndText = "Health check {HealthCheckName} with status {HealthStatus} completed after {ElapsedMilliseconds}ms with message '{HealthCheckDescription}'" ;
215
224
216
225
#pragma warning disable SYSLIB1006
217
- [ LoggerMessage ( EventId = EventIds . HealthCheckEndId , Level = LogLevel . Debug , Message = HealthCheckEndText ) ]
218
- private partial void HealthCheckEndHealthy ( string HealthCheckName , HealthStatus HealthStatus , double ElapsedMilliseconds , string ? HealthCheckDescription ) ;
226
+ [ LoggerMessage ( EventIds . HealthCheckEndId , LogLevel . Debug , HealthCheckEndText , EventName = EventIds . HealthCheckEndName ) ]
227
+ private static partial void HealthCheckEndHealthy ( ILogger logger , string HealthCheckName , HealthStatus HealthStatus , double ElapsedMilliseconds , string ? HealthCheckDescription ) ;
219
228
220
- [ LoggerMessage ( EventId = EventIds . HealthCheckEndId , Level = LogLevel . Warning , Message = HealthCheckEndText ) ]
221
- private partial void HealthCheckEndDegraded ( string HealthCheckName , HealthStatus HealthStatus , double ElapsedMilliseconds , string ? HealthCheckDescription ) ;
229
+ [ LoggerMessage ( EventIds . HealthCheckEndId , LogLevel . Warning , HealthCheckEndText , EventName = EventIds . HealthCheckEndName ) ]
230
+ private static partial void HealthCheckEndDegraded ( ILogger logger , string HealthCheckName , HealthStatus HealthStatus , double ElapsedMilliseconds , string ? HealthCheckDescription ) ;
222
231
223
- [ LoggerMessage ( EventId = EventIds . HealthCheckEndId , Level = LogLevel . Error , Message = HealthCheckEndText ) ]
224
- private partial void HealthCheckEndUnhealthy ( string HealthCheckName , HealthStatus HealthStatus , double ElapsedMilliseconds , string ? HealthCheckDescription , Exception ? exception ) ;
232
+ [ LoggerMessage ( EventIds . HealthCheckEndId , LogLevel . Error , HealthCheckEndText , EventName = EventIds . HealthCheckEndName ) ]
233
+ private static partial void HealthCheckEndUnhealthy ( ILogger logger , string HealthCheckName , HealthStatus HealthStatus , double ElapsedMilliseconds , string ? HealthCheckDescription , Exception ? exception ) ;
225
234
#pragma warning restore SYSLIB1006
226
235
227
- private void HealthCheckEnd ( HealthCheckRegistration registration , HealthReportEntry entry , TimeSpan duration )
228
- {
229
- switch ( entry . Status )
236
+ public static void HealthCheckEnd ( ILogger logger , HealthCheckRegistration registration , HealthReportEntry entry , TimeSpan duration )
230
237
{
231
- case HealthStatus . Healthy :
232
- HealthCheckEndHealthy ( registration . Name , entry . Status , duration . TotalMilliseconds , entry . Description ) ;
233
- break ;
238
+ switch ( entry . Status )
239
+ {
240
+ case HealthStatus . Healthy :
241
+ HealthCheckEndHealthy ( logger , registration . Name , entry . Status , duration . TotalMilliseconds , entry . Description ) ;
242
+ break ;
234
243
235
- case HealthStatus . Degraded :
236
- HealthCheckEndDegraded ( registration . Name , entry . Status , duration . TotalMilliseconds , entry . Description ) ;
237
- break ;
244
+ case HealthStatus . Degraded :
245
+ HealthCheckEndDegraded ( logger , registration . Name , entry . Status , duration . TotalMilliseconds , entry . Description ) ;
246
+ break ;
238
247
239
- case HealthStatus . Unhealthy :
240
- HealthCheckEndUnhealthy ( registration . Name , entry . Status , duration . TotalMilliseconds , entry . Description , entry . Exception ) ;
241
- break ;
248
+ case HealthStatus . Unhealthy :
249
+ HealthCheckEndUnhealthy ( logger , registration . Name , entry . Status , duration . TotalMilliseconds , entry . Description , entry . Exception ) ;
250
+ break ;
251
+ }
242
252
}
243
- }
244
253
245
- [ LoggerMessage ( EventId = EventIds . HealthCheckErrorId , Level = LogLevel . Error , Message = "Health check {HealthCheckName} threw an unhandled exception after {ElapsedMilliseconds}ms" ) ]
246
- private partial void HealthCheckError ( string HealthCheckName , double ElapsedMilliseconds , Exception exception ) ;
254
+ [ LoggerMessage ( EventIds . HealthCheckErrorId , LogLevel . Error , "Health check {HealthCheckName} threw an unhandled exception after {ElapsedMilliseconds}ms" , EventName = EventIds . HealthCheckErrorName ) ]
255
+ private static partial void HealthCheckError ( ILogger logger , string HealthCheckName , double ElapsedMilliseconds , Exception exception ) ;
247
256
248
- private void HealthCheckError ( HealthCheckRegistration registration , Exception exception , TimeSpan duration ) =>
249
- HealthCheckError ( registration . Name , duration . TotalMilliseconds , exception ) ;
257
+ public static void HealthCheckError ( ILogger logger , HealthCheckRegistration registration , Exception exception , TimeSpan duration ) =>
258
+ HealthCheckError ( logger , registration . Name , duration . TotalMilliseconds , exception ) ;
250
259
251
- private void HealthCheckData ( HealthCheckRegistration registration , HealthReportEntry entry )
252
- {
253
- if ( entry . Data . Count > 0 && _logger . IsEnabled ( LogLevel . Debug ) )
260
+ public static void HealthCheckData ( ILogger logger , HealthCheckRegistration registration , HealthReportEntry entry )
254
261
{
255
- _logger . Log (
256
- LogLevel . Debug ,
257
- EventIds . HealthCheckData ,
258
- new HealthCheckDataLogValue ( registration . Name , entry . Data ) ,
259
- null ,
260
- ( state , ex ) => state . ToString ( ) ) ;
262
+ if ( entry . Data . Count > 0 && logger . IsEnabled ( LogLevel . Debug ) )
263
+ {
264
+ logger . Log (
265
+ LogLevel . Debug ,
266
+ EventIds . HealthCheckData ,
267
+ new HealthCheckDataLogValue ( registration . Name , entry . Data ) ,
268
+ null ,
269
+ ( state , ex ) => state . ToString ( ) ) ;
270
+ }
261
271
}
262
272
}
263
273
0 commit comments