|
15 | 15 |
|
16 | 16 | namespace Microsoft.Extensions.Diagnostics.HealthChecks |
17 | 17 | { |
18 | | - internal class DefaultHealthCheckService : HealthCheckService |
| 18 | + internal partial class DefaultHealthCheckService : HealthCheckService |
19 | 19 | { |
20 | 20 | private readonly IServiceScopeFactory _scopeFactory; |
21 | 21 | private readonly IOptions<HealthCheckServiceOptions> _options; |
@@ -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 | | - Log.HealthCheckBegin(_logger, registration); |
| 90 | + Log.HealthCheckBegin(_logger, registration.Name); |
91 | 91 |
|
92 | 92 | HealthReportEntry entry; |
93 | 93 | CancellationTokenSource? timeoutCancellationTokenSource = null; |
@@ -182,92 +182,75 @@ private static void ValidateRegistrations(IEnumerable<HealthCheckRegistration> r |
182 | 182 |
|
183 | 183 | internal static class EventIds |
184 | 184 | { |
185 | | - public static readonly EventId HealthCheckProcessingBegin = new EventId(100, "HealthCheckProcessingBegin"); |
186 | | - public static readonly EventId HealthCheckProcessingEnd = new EventId(101, "HealthCheckProcessingEnd"); |
187 | | - |
188 | | - public static readonly EventId HealthCheckBegin = new EventId(102, "HealthCheckBegin"); |
189 | | - public static readonly EventId HealthCheckEnd = new EventId(103, "HealthCheckEnd"); |
190 | | - public static readonly EventId HealthCheckError = new EventId(104, "HealthCheckError"); |
191 | | - public static readonly EventId HealthCheckData = new EventId(105, "HealthCheckData"); |
| 185 | + public const int HealthCheckProcessingBeginId = 100; |
| 186 | + public const int HealthCheckProcessingEndId = 101; |
| 187 | + public const int HealthCheckBeginId = 102; |
| 188 | + public const int HealthCheckEndId = 103; |
| 189 | + public const int HealthCheckErrorId = 104; |
| 190 | + public const int HealthCheckDataId = 105; |
| 191 | + |
| 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 HealthCheckData = new EventId(HealthCheckDataId, HealthCheckDataName); |
192 | 201 | } |
193 | 202 |
|
194 | | - private static class Log |
| 203 | + private static partial class Log |
195 | 204 | { |
196 | | - private static readonly Action<ILogger, Exception?> _healthCheckProcessingBegin = LoggerMessage.Define( |
197 | | - LogLevel.Debug, |
198 | | - EventIds.HealthCheckProcessingBegin, |
199 | | - "Running health checks"); |
| 205 | + [LoggerMessage(EventIds.HealthCheckProcessingBeginId, LogLevel.Debug, "Running health checks", EventName = EventIds.HealthCheckProcessingBeginName)] |
| 206 | + public static partial void HealthCheckProcessingBegin(ILogger logger); |
| 207 | + |
| 208 | + public static void HealthCheckProcessingEnd(ILogger logger, HealthStatus status, TimeSpan duration) => |
| 209 | + HealthCheckProcessingEnd(logger, status, duration.TotalMilliseconds); |
200 | 210 |
|
201 | | - private static readonly Action<ILogger, double, HealthStatus, Exception?> _healthCheckProcessingEnd = LoggerMessage.Define<double, HealthStatus>( |
202 | | - LogLevel.Debug, |
203 | | - EventIds.HealthCheckProcessingEnd, |
204 | | - "Health check processing with combined status {HealthStatus} completed after {ElapsedMilliseconds}ms"); |
| 211 | + [LoggerMessage(EventIds.HealthCheckProcessingEndId, LogLevel.Debug, "Health check processing with combined status {HealthStatus} completed after {ElapsedMilliseconds}ms", EventName = EventIds.HealthCheckProcessingEndName)] |
| 212 | + private static partial void HealthCheckProcessingEnd(ILogger logger, HealthStatus HealthStatus, double ElapsedMilliseconds); |
205 | 213 |
|
206 | | - private static readonly Action<ILogger, string, Exception?> _healthCheckBegin = LoggerMessage.Define<string>( |
207 | | - LogLevel.Debug, |
208 | | - EventIds.HealthCheckBegin, |
209 | | - "Running health check {HealthCheckName}"); |
| 214 | + [LoggerMessage(EventIds.HealthCheckBeginId, LogLevel.Debug, "Running health check {HealthCheckName}", EventName = EventIds.HealthCheckBeginName)] |
| 215 | + public static partial void HealthCheckBegin(ILogger logger, string HealthCheckName); |
210 | 216 |
|
211 | 217 | // These are separate so they can have different log levels |
212 | 218 | private const string HealthCheckEndText = "Health check {HealthCheckName} with status {HealthStatus} completed after {ElapsedMilliseconds}ms with message '{HealthCheckDescription}'"; |
213 | 219 |
|
214 | | - private static readonly Action<ILogger, string, double, HealthStatus, string?, Exception?> _healthCheckEndHealthy = LoggerMessage.Define<string, double, HealthStatus, string?>( |
215 | | - LogLevel.Debug, |
216 | | - EventIds.HealthCheckEnd, |
217 | | - HealthCheckEndText); |
218 | | - |
219 | | - private static readonly Action<ILogger, string, double, HealthStatus, string?, Exception?> _healthCheckEndDegraded = LoggerMessage.Define<string, double, HealthStatus, string?>( |
220 | | - LogLevel.Warning, |
221 | | - EventIds.HealthCheckEnd, |
222 | | - HealthCheckEndText); |
223 | | - |
224 | | - private static readonly Action<ILogger, string, double, HealthStatus, string?, Exception?> _healthCheckEndUnhealthy = LoggerMessage.Define<string, double, HealthStatus, string?>( |
225 | | - LogLevel.Error, |
226 | | - EventIds.HealthCheckEnd, |
227 | | - HealthCheckEndText); |
| 220 | +#pragma warning disable SYSLIB1006 |
| 221 | + [LoggerMessage(EventIds.HealthCheckEndId, LogLevel.Debug, HealthCheckEndText, EventName = EventIds.HealthCheckEndName)] |
| 222 | + private static partial void HealthCheckEndHealthy(ILogger logger, string HealthCheckName, HealthStatus HealthStatus, double ElapsedMilliseconds, string? HealthCheckDescription); |
228 | 223 |
|
229 | | - private static readonly Action<ILogger, string, double, Exception?> _healthCheckError = LoggerMessage.Define<string, double>( |
230 | | - LogLevel.Error, |
231 | | - EventIds.HealthCheckError, |
232 | | - "Health check {HealthCheckName} threw an unhandled exception after {ElapsedMilliseconds}ms"); |
| 224 | + [LoggerMessage(EventIds.HealthCheckEndId, LogLevel.Warning, HealthCheckEndText, EventName = EventIds.HealthCheckEndName)] |
| 225 | + private static partial void HealthCheckEndDegraded(ILogger logger, string HealthCheckName, HealthStatus HealthStatus, double ElapsedMilliseconds, string? HealthCheckDescription); |
233 | 226 |
|
234 | | - public static void HealthCheckProcessingBegin(ILogger logger) |
235 | | - { |
236 | | - _healthCheckProcessingBegin(logger, null); |
237 | | - } |
238 | | - |
239 | | - public static void HealthCheckProcessingEnd(ILogger logger, HealthStatus status, TimeSpan duration) |
240 | | - { |
241 | | - _healthCheckProcessingEnd(logger, duration.TotalMilliseconds, status, null); |
242 | | - } |
243 | | - |
244 | | - public static void HealthCheckBegin(ILogger logger, HealthCheckRegistration registration) |
245 | | - { |
246 | | - _healthCheckBegin(logger, registration.Name, null); |
247 | | - } |
| 227 | + [LoggerMessage(EventIds.HealthCheckEndId, LogLevel.Error, HealthCheckEndText, EventName = EventIds.HealthCheckEndName)] |
| 228 | + private static partial void HealthCheckEndUnhealthy(ILogger logger, string HealthCheckName, HealthStatus HealthStatus, double ElapsedMilliseconds, string? HealthCheckDescription, Exception? exception); |
| 229 | +#pragma warning restore SYSLIB1006 |
248 | 230 |
|
249 | 231 | public static void HealthCheckEnd(ILogger logger, HealthCheckRegistration registration, HealthReportEntry entry, TimeSpan duration) |
250 | 232 | { |
251 | 233 | switch (entry.Status) |
252 | 234 | { |
253 | 235 | case HealthStatus.Healthy: |
254 | | - _healthCheckEndHealthy(logger, registration.Name, duration.TotalMilliseconds, entry.Status, entry.Description, null); |
| 236 | + HealthCheckEndHealthy(logger, registration.Name, entry.Status, duration.TotalMilliseconds, entry.Description); |
255 | 237 | break; |
256 | 238 |
|
257 | 239 | case HealthStatus.Degraded: |
258 | | - _healthCheckEndDegraded(logger, registration.Name, duration.TotalMilliseconds, entry.Status, entry.Description, null); |
| 240 | + HealthCheckEndDegraded(logger, registration.Name, entry.Status, duration.TotalMilliseconds, entry.Description); |
259 | 241 | break; |
260 | 242 |
|
261 | 243 | case HealthStatus.Unhealthy: |
262 | | - _healthCheckEndUnhealthy(logger, registration.Name, duration.TotalMilliseconds, entry.Status, entry.Description, entry.Exception); |
| 244 | + HealthCheckEndUnhealthy(logger, registration.Name, entry.Status, duration.TotalMilliseconds, entry.Description, entry.Exception); |
263 | 245 | break; |
264 | 246 | } |
265 | 247 | } |
266 | 248 |
|
267 | | - public static void HealthCheckError(ILogger logger, HealthCheckRegistration registration, Exception exception, TimeSpan duration) |
268 | | - { |
269 | | - _healthCheckError(logger, registration.Name, duration.TotalMilliseconds, exception); |
270 | | - } |
| 249 | + [LoggerMessage(EventIds.HealthCheckErrorId, LogLevel.Error, "Health check {HealthCheckName} threw an unhandled exception after {ElapsedMilliseconds}ms", EventName = EventIds.HealthCheckErrorName)] |
| 250 | + private static partial void HealthCheckError(ILogger logger, string HealthCheckName, double ElapsedMilliseconds, Exception exception); |
| 251 | + |
| 252 | + public static void HealthCheckError(ILogger logger, HealthCheckRegistration registration, Exception exception, TimeSpan duration) => |
| 253 | + HealthCheckError(logger, registration.Name, duration.TotalMilliseconds, exception); |
271 | 254 |
|
272 | 255 | public static void HealthCheckData(ILogger logger, HealthCheckRegistration registration, HealthReportEntry entry) |
273 | 256 | { |
|
0 commit comments