diff --git a/TinyHealthCheck/HealthCheckService.cs b/TinyHealthCheck/HealthCheckService.cs index 6a77b04..4529ff1 100644 --- a/TinyHealthCheck/HealthCheckService.cs +++ b/TinyHealthCheck/HealthCheckService.cs @@ -16,12 +16,16 @@ 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 +40,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<{TypeName}> started on port {Port}", _typeName, _config.Port); var cancelTask = Task.Delay(Timeout.Infinite, cancellationToken); while (!cancellationToken.IsCancellationRequested) @@ -49,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); } } @@ -62,8 +66,8 @@ protected override async Task ExecuteAsync(CancellationToken cancellationToken) private async Task ProcessHealthCheck(HttpListenerContext client, CancellationToken cancellationToken) { var request = client.Request; - - _logger.LogInformation($"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) { 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; } }