Skip to content

Commit

Permalink
Merge pull request #5 from Twinki14/configurable-log-levels
Browse files Browse the repository at this point in the history
Add LogLevel to HealthCheckConfig for configurable logging levels for a Health Check
  • Loading branch information
bruceharrison1984 authored Dec 9, 2023
2 parents 8afc429 + 43884ca commit d92f879
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
12 changes: 8 additions & 4 deletions TinyHealthCheck/HealthCheckService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,16 @@ public class HealthCheckService<T> : 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<HealthCheckService<T>> logger, T healthCheck, TinyHealthCheckConfig config)
{
_logger = logger;
_config = config ?? throw new ArgumentNullException(nameof(config));
_healthCheck = healthCheck;

_typeName = typeof(T).Name;
}

/// <summary>
Expand All @@ -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)
Expand All @@ -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);
}
}

Expand All @@ -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)
{
Expand Down
7 changes: 6 additions & 1 deletion TinyHealthCheck/Models/TinyHealthCheckConfig.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Text;
using Microsoft.Extensions.Logging;

namespace TinyHealthCheck.Models
{
Expand All @@ -19,5 +19,10 @@ public class TinyHealthCheckConfig
/// The url path that the health check will be accessible on.
/// </summary>
public string UrlPath { get; set; } = "/healthz";

/// <summary>
/// The logging level of the health check.
/// </summary>
public LogLevel LogLevel = LogLevel.Information;
}
}

0 comments on commit d92f879

Please sign in to comment.