|
15 | 15 |
|
16 | 16 | namespace Microsoft.Extensions.Diagnostics.HealthChecks
|
17 | 17 | {
|
18 |
| - internal partial class DefaultHealthCheckService : HealthCheckService |
| 18 | + internal 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.Name); |
| 90 | + Log.HealthCheckBegin(_logger, registration); |
91 | 91 |
|
92 | 92 | HealthReportEntry entry;
|
93 | 93 | CancellationTokenSource? timeoutCancellationTokenSource = null;
|
@@ -182,75 +182,92 @@ private static void ValidateRegistrations(IEnumerable<HealthCheckRegistration> r
|
182 | 182 |
|
183 | 183 | internal static class EventIds
|
184 | 184 | {
|
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); |
| 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"); |
201 | 192 | }
|
202 | 193 |
|
203 |
| - private static partial class Log |
| 194 | + private static class Log |
204 | 195 | {
|
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); |
| 196 | + private static readonly Action<ILogger, Exception?> _healthCheckProcessingBegin = LoggerMessage.Define( |
| 197 | + LogLevel.Debug, |
| 198 | + EventIds.HealthCheckProcessingBegin, |
| 199 | + "Running health checks"); |
210 | 200 |
|
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); |
| 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"); |
213 | 205 |
|
214 |
| - [LoggerMessage(EventIds.HealthCheckBeginId, LogLevel.Debug, "Running health check {HealthCheckName}", EventName = EventIds.HealthCheckBeginName)] |
215 |
| - public static partial void HealthCheckBegin(ILogger logger, string HealthCheckName); |
| 206 | + private static readonly Action<ILogger, string, Exception?> _healthCheckBegin = LoggerMessage.Define<string>( |
| 207 | + LogLevel.Debug, |
| 208 | + EventIds.HealthCheckBegin, |
| 209 | + "Running health check {HealthCheckName}"); |
216 | 210 |
|
217 | 211 | // These are separate so they can have different log levels
|
218 | 212 | private const string HealthCheckEndText = "Health check {HealthCheckName} with status {HealthStatus} completed after {ElapsedMilliseconds}ms with message '{HealthCheckDescription}'";
|
219 | 213 |
|
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); |
| 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); |
223 | 228 |
|
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); |
| 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"); |
226 | 233 |
|
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 |
| 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 | + } |
230 | 248 |
|
231 | 249 | public static void HealthCheckEnd(ILogger logger, HealthCheckRegistration registration, HealthReportEntry entry, TimeSpan duration)
|
232 | 250 | {
|
233 | 251 | switch (entry.Status)
|
234 | 252 | {
|
235 | 253 | case HealthStatus.Healthy:
|
236 |
| - HealthCheckEndHealthy(logger, registration.Name, entry.Status, duration.TotalMilliseconds, entry.Description); |
| 254 | + _healthCheckEndHealthy(logger, registration.Name, duration.TotalMilliseconds, entry.Status, entry.Description, null); |
237 | 255 | break;
|
238 | 256 |
|
239 | 257 | case HealthStatus.Degraded:
|
240 |
| - HealthCheckEndDegraded(logger, registration.Name, entry.Status, duration.TotalMilliseconds, entry.Description); |
| 258 | + _healthCheckEndDegraded(logger, registration.Name, duration.TotalMilliseconds, entry.Status, entry.Description, null); |
241 | 259 | break;
|
242 | 260 |
|
243 | 261 | case HealthStatus.Unhealthy:
|
244 |
| - HealthCheckEndUnhealthy(logger, registration.Name, entry.Status, duration.TotalMilliseconds, entry.Description, entry.Exception); |
| 262 | + _healthCheckEndUnhealthy(logger, registration.Name, duration.TotalMilliseconds, entry.Status, entry.Description, entry.Exception); |
245 | 263 | break;
|
246 | 264 | }
|
247 | 265 | }
|
248 | 266 |
|
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); |
| 267 | + public static void HealthCheckError(ILogger logger, HealthCheckRegistration registration, Exception exception, TimeSpan duration) |
| 268 | + { |
| 269 | + _healthCheckError(logger, registration.Name, duration.TotalMilliseconds, exception); |
| 270 | + } |
254 | 271 |
|
255 | 272 | public static void HealthCheckData(ILogger logger, HealthCheckRegistration registration, HealthReportEntry entry)
|
256 | 273 | {
|
|
0 commit comments