From 9191a26091da7e95bc3adecfb4e711e016c35acc Mon Sep 17 00:00:00 2001 From: Twinki Date: Tue, 5 Dec 2023 17:12:40 -0500 Subject: [PATCH 1/4] Add LogLevel to health check config --- TinyHealthCheck/HealthCheckService.cs | 4 ++-- TinyHealthCheck/Models/TinyHealthCheckConfig.cs | 7 ++++++- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/TinyHealthCheck/HealthCheckService.cs b/TinyHealthCheck/HealthCheckService.cs index 6a77b04..5ea5c03 100644 --- a/TinyHealthCheck/HealthCheckService.cs +++ b/TinyHealthCheck/HealthCheckService.cs @@ -36,7 +36,7 @@ protected override async Task ExecuteAsync(CancellationToken cancellationToken) _listener.Prefixes.Add($"http://{_config.Hostname}:{_config.Port}/"); _listener.Start(); - _logger.LogInformation($"TinyHealthCheck<{typeof(T).Name}> started on port '{_config.Port}'"); + _logger.Log(_config.LogLevel, $"TinyHealthCheck<{typeof(T).Name}> started on port '{_config.Port}'"); var cancelTask = Task.Delay(Timeout.Infinite, cancellationToken); while (!cancellationToken.IsCancellationRequested) @@ -63,7 +63,7 @@ private async Task ProcessHealthCheck(HttpListenerContext client, CancellationTo { var request = client.Request; - _logger.LogInformation($"TinyHealthCheck received a request from {request.RemoteEndPoint}"); + _logger.Log(_config.LogLevel, $"TinyHealthCheck received a request from {request.RemoteEndPoint}"); using (var response = client.Response) { diff --git a/TinyHealthCheck/Models/TinyHealthCheckConfig.cs b/TinyHealthCheck/Models/TinyHealthCheckConfig.cs index 5efeff5..8b839e2 100644 --- a/TinyHealthCheck/Models/TinyHealthCheckConfig.cs +++ b/TinyHealthCheck/Models/TinyHealthCheckConfig.cs @@ -1,4 +1,4 @@ -using System.Text; +using Microsoft.Extensions.Logging; namespace TinyHealthCheck.Models { @@ -19,5 +19,10 @@ public class TinyHealthCheckConfig /// The url path that the health check will be accessible on. /// public string UrlPath { get; set; } = "/healthz"; + + /// + /// The logging level of the health check. + /// + public LogLevel LogLevel = LogLevel.Information; } } From 2b644672c43b42d4726a0ef5901c09e0e44edf87 Mon Sep 17 00:00:00 2001 From: Twinki Date: Tue, 5 Dec 2023 17:17:15 -0500 Subject: [PATCH 2/4] Update HealthCheckService.cs --- TinyHealthCheck/HealthCheckService.cs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/TinyHealthCheck/HealthCheckService.cs b/TinyHealthCheck/HealthCheckService.cs index 5ea5c03..515512d 100644 --- a/TinyHealthCheck/HealthCheckService.cs +++ b/TinyHealthCheck/HealthCheckService.cs @@ -16,12 +16,15 @@ public class HealthCheckService : BackgroundService where T : IHealthCheck private readonly TinyHealthCheckConfig _config; private readonly T _healthCheck; private readonly HttpListener _listener = new HttpListener(); + private readonly string _typeName; public HealthCheckService(ILogger> logger, T healthCheck, TinyHealthCheckConfig config) { _logger = logger; _config = config ?? throw new ArgumentNullException(nameof(config)); _healthCheck = healthCheck; + + _typeName = typeof(T).Name; } /// @@ -36,7 +39,7 @@ protected override async Task ExecuteAsync(CancellationToken cancellationToken) _listener.Prefixes.Add($"http://{_config.Hostname}:{_config.Port}/"); _listener.Start(); - _logger.Log(_config.LogLevel, $"TinyHealthCheck<{typeof(T).Name}> started on port '{_config.Port}'"); + _logger.Log(_config.LogLevel, "TinyHealthCheck<{TypeName}> started on port {Port}", _typeName, _config.Port); var cancelTask = Task.Delay(Timeout.Infinite, cancellationToken); while (!cancellationToken.IsCancellationRequested) @@ -62,8 +65,8 @@ protected override async Task ExecuteAsync(CancellationToken cancellationToken) private async Task ProcessHealthCheck(HttpListenerContext client, CancellationToken cancellationToken) { var request = client.Request; - - _logger.Log(_config.LogLevel, $"TinyHealthCheck received a request from {request.RemoteEndPoint}"); + + _logger.Log(_config.LogLevel, "TinyHealthCheck<{TypeName}> received a request from {RequestEndpoint}", _typeName, request.RemoteEndPoint); using (var response = client.Response) { From bfb2f0bd6922b5de954754cf01e83bb5e4c1afe3 Mon Sep 17 00:00:00 2001 From: Twinki Date: Tue, 5 Dec 2023 17:17:22 -0500 Subject: [PATCH 3/4] Update HealthCheckService.cs --- TinyHealthCheck/HealthCheckService.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/TinyHealthCheck/HealthCheckService.cs b/TinyHealthCheck/HealthCheckService.cs index 515512d..d590769 100644 --- a/TinyHealthCheck/HealthCheckService.cs +++ b/TinyHealthCheck/HealthCheckService.cs @@ -16,6 +16,7 @@ public class HealthCheckService : BackgroundService where T : IHealthCheck private readonly TinyHealthCheckConfig _config; private readonly T _healthCheck; private readonly HttpListener _listener = new HttpListener(); + private readonly string _typeName; public HealthCheckService(ILogger> logger, T healthCheck, TinyHealthCheckConfig config) From 43884cac62eb22395de0058b8ea525d776622750 Mon Sep 17 00:00:00 2001 From: Twinki Date: Tue, 5 Dec 2023 17:22:08 -0500 Subject: [PATCH 4/4] adjust exception logging too --- TinyHealthCheck/HealthCheckService.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/TinyHealthCheck/HealthCheckService.cs b/TinyHealthCheck/HealthCheckService.cs index d590769..4529ff1 100644 --- a/TinyHealthCheck/HealthCheckService.cs +++ b/TinyHealthCheck/HealthCheckService.cs @@ -53,7 +53,7 @@ protected override async Task ExecuteAsync(CancellationToken cancellationToken) } catch (Exception e) { - _logger.LogError(e, "TinyHealthCheck had an exception!"); + _logger.LogError(e, "TinyHealthCheck<{TypeName}> encountered an exception!", _typeName); } }