@@ -46,7 +46,7 @@ public override async Task<HealthReport> CheckHealthAsync(
4646 }
4747
4848 var totalTime = ValueStopwatch . StartNew ( ) ;
49- HealthCheckProcessingBegin ( ) ;
49+ Log . HealthCheckProcessingBegin ( _logger ) ;
5050
5151 var tasks = new Task < HealthReportEntry > [ registrations . Count ] ;
5252 var index = 0 ;
@@ -68,7 +68,7 @@ public override async Task<HealthReport> CheckHealthAsync(
6868
6969 var totalElapsedTime = totalTime . GetElapsedTime ( ) ;
7070 var report = new HealthReport ( entries , totalElapsedTime ) ;
71- HealthCheckProcessingEnd ( report . Status , totalElapsedTime ) ;
71+ Log . HealthCheckProcessingEnd ( _logger , report . Status , totalElapsedTime ) ;
7272 return report ;
7373 }
7474
@@ -87,7 +87,7 @@ private async Task<HealthReportEntry> RunCheckAsync(HealthCheckRegistration regi
8787 var stopwatch = ValueStopwatch . StartNew ( ) ;
8888 var context = new HealthCheckContext { Registration = registration } ;
8989
90- HealthCheckBegin ( registration . Name ) ;
90+ Log . HealthCheckBegin ( _logger , registration . Name ) ;
9191
9292 HealthReportEntry entry ;
9393 CancellationTokenSource ? timeoutCancellationTokenSource = null ;
@@ -115,8 +115,8 @@ private async Task<HealthReportEntry> RunCheckAsync(HealthCheckRegistration regi
115115 data : result . Data ,
116116 tags : registration . Tags ) ;
117117
118- HealthCheckEnd ( registration , entry , duration ) ;
119- HealthCheckData ( registration , entry ) ;
118+ Log . HealthCheckEnd ( _logger , registration , entry , duration ) ;
119+ Log . HealthCheckData ( _logger , registration , entry ) ;
120120 }
121121 catch ( OperationCanceledException ex ) when ( ! cancellationToken . IsCancellationRequested )
122122 {
@@ -129,7 +129,7 @@ private async Task<HealthReportEntry> RunCheckAsync(HealthCheckRegistration regi
129129 data : null ,
130130 tags : registration . Tags ) ;
131131
132- HealthCheckError ( registration , ex , duration ) ;
132+ Log . HealthCheckError ( _logger , registration , ex , duration ) ;
133133 }
134134
135135 // Allow cancellation to propagate if it's not a timeout.
@@ -144,7 +144,7 @@ private async Task<HealthReportEntry> RunCheckAsync(HealthCheckRegistration regi
144144 data : null ,
145145 tags : registration . Tags ) ;
146146
147- HealthCheckError ( registration , ex , duration ) ;
147+ Log . HealthCheckError ( _logger , registration , ex , duration ) ;
148148 }
149149
150150 finally
@@ -189,75 +189,85 @@ internal static class EventIds
189189 public const int HealthCheckErrorId = 104 ;
190190 public const int HealthCheckDataId = 105 ;
191191
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 ) ;
199206 }
200207
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 ) ;
203212
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 ) ;
206215
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 ) ;
209218
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 ) ;
212221
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}'" ;
215224
216225#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 ) ;
219228
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 ) ;
222231
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 ) ;
225234#pragma warning restore SYSLIB1006
226235
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 )
230237 {
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 ;
234243
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 ;
238247
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+ }
242252 }
243- }
244253
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 ) ;
247256
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 ) ;
250259
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 )
254261 {
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+ }
261271 }
262272 }
263273
0 commit comments