From 672a4f810ca8390027bf06eed4686eb381d24e26 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karl-Johan=20Sj=C3=B6gren?= Date: Mon, 2 Dec 2019 15:37:41 +0100 Subject: [PATCH] Added a separate endpoint for named checks in the sample, updated logging to use the new name property --- samples/MvcSample/Startup.cs | 5 ++-- .../RimDevAspNetHealthCheckService.cs | 28 +++++++++---------- 2 files changed, 16 insertions(+), 17 deletions(-) diff --git a/samples/MvcSample/Startup.cs b/samples/MvcSample/Startup.cs index e920f2e..fac480d 100644 --- a/samples/MvcSample/Startup.cs +++ b/samples/MvcSample/Startup.cs @@ -21,12 +21,11 @@ public void Configuration(IAppBuilder app) // "select 'a'"), new PingHealthCheck(new PingHealthCheckOptions().AddHost("localhost", 1000))); - /* Sample with named checks + // Sample with named checks app.UseHealthChecks( - "/_health", + "/_health_named", new HealthCheckWrapper(new NoopHealthCheck(), "Noop health check"), new HealthCheckWrapper(new PingHealthCheck(new PingHealthCheckOptions().AddHost("localhost", 1000)), "Ping to localhost")); - */ } } diff --git a/src/RimDev.AspNet.Diagnostics.HealthChecks/RimDevAspNetHealthCheckService.cs b/src/RimDev.AspNet.Diagnostics.HealthChecks/RimDevAspNetHealthCheckService.cs index 6585c1f..7fd34ef 100644 --- a/src/RimDev.AspNet.Diagnostics.HealthChecks/RimDevAspNetHealthCheckService.cs +++ b/src/RimDev.AspNet.Diagnostics.HealthChecks/RimDevAspNetHealthCheckService.cs @@ -52,7 +52,7 @@ public async Task CheckHealthAsync( { var stopwatch = Stopwatch.StartNew(); - Log.HealthCheckBegin(_logger, wrapper.HealthCheck); + Log.HealthCheckBegin(_logger, wrapper); HealthReportEntry entry; try @@ -76,8 +76,8 @@ public async Task CheckHealthAsync( exception: result.Exception, data: result.Data); - Log.HealthCheckEnd(_logger, wrapper.HealthCheck, entry, duration); - Log.HealthCheckData(_logger, wrapper.HealthCheck, entry); + Log.HealthCheckEnd(_logger, wrapper, entry, duration); + Log.HealthCheckData(_logger, wrapper, entry); } // Allow cancellation to propagate. @@ -91,7 +91,7 @@ public async Task CheckHealthAsync( exception: ex, data: null); - Log.HealthCheckError(_logger, wrapper.HealthCheck, ex, duration); + Log.HealthCheckError(_logger, wrapper, ex, duration); } entries[wrapper.Name] = entry; @@ -173,42 +173,42 @@ public static void HealthCheckProcessingEnd(ILogger logger, HealthStatus status, _healthCheckProcessingEnd(logger, duration.TotalMilliseconds, status, null); } - public static void HealthCheckBegin(ILogger logger, IHealthCheck healthCheck) + public static void HealthCheckBegin(ILogger logger, HealthCheckWrapper healthCheck) { - _healthCheckBegin(logger, healthCheck.GetType().Name, null); + _healthCheckBegin(logger, healthCheck.Name, null); } - public static void HealthCheckEnd(ILogger logger, IHealthCheck healthCheck, HealthReportEntry entry, TimeSpan duration) + public static void HealthCheckEnd(ILogger logger, HealthCheckWrapper healthCheck, HealthReportEntry entry, TimeSpan duration) { switch (entry.Status) { case HealthStatus.Healthy: - _healthCheckEndHealthy(logger, healthCheck.GetType().Name, duration.TotalMilliseconds, entry.Status, entry.Description, null); + _healthCheckEndHealthy(logger, healthCheck.Name, duration.TotalMilliseconds, entry.Status, entry.Description, null); break; case HealthStatus.Degraded: - _healthCheckEndDegraded(logger, healthCheck.GetType().Name, duration.TotalMilliseconds, entry.Status, entry.Description, null); + _healthCheckEndDegraded(logger, healthCheck.Name, duration.TotalMilliseconds, entry.Status, entry.Description, null); break; case HealthStatus.Unhealthy: - _healthCheckEndUnhealthy(logger, healthCheck.GetType().Name, duration.TotalMilliseconds, entry.Status, entry.Description, null); + _healthCheckEndUnhealthy(logger, healthCheck.Name, duration.TotalMilliseconds, entry.Status, entry.Description, null); break; } } - public static void HealthCheckError(ILogger logger, IHealthCheck healthCheck, Exception exception, TimeSpan duration) + public static void HealthCheckError(ILogger logger, HealthCheckWrapper healthCheck, Exception exception, TimeSpan duration) { - _healthCheckError(logger, healthCheck.GetType().Name, duration.TotalMilliseconds, exception); + _healthCheckError(logger, healthCheck.Name, duration.TotalMilliseconds, exception); } - public static void HealthCheckData(ILogger logger, IHealthCheck healthCheck, HealthReportEntry entry) + public static void HealthCheckData(ILogger logger, HealthCheckWrapper healthCheck, HealthReportEntry entry) { if (entry.Data.Count > 0 && logger.IsEnabled(LogLevel.Debug)) { logger.Log( LogLevel.Debug, EventIds.HealthCheckData, - new HealthCheckDataLogValue(healthCheck.GetType().Name, entry.Data), + new HealthCheckDataLogValue(healthCheck.Name, entry.Data), null, (state, ex) => state.ToString()); }