asp.net-coreseriloghealth-checkasp.net-core-8serilog-aspnetcore

Prevent noisy log events with ASP.NET Core Healthchecks and Serilog


I want to use healthchecks and Serilog (with request logging), but I get a flood of useless log events, as well as ones for the failure cases.

The v8 docs (and this) show there are differences between UseHealthChecks and MapHealthChecks. I prefer the latter (which is the recommended option as per the docs).

I think the middleware order must be the problem. What is the correct order?


Solution

  • I found some guidance here, here and here.

    Apparently when using MapHealthChecks one cannot simply rely on the position of the middleware in the pipeline; one must also use short-circuiting.

    The middleware pipeline should be configured in this order:

    // must be before serilog so not logged
    app.UseStaticFiles();
    
    // must be before healthchecks, so can use `ShortCircuit`
    app.UseRouting();
    
    // can be before serilog...
    app.MapHealthChecks("/healthz")
      .ShortCircuit();                // prevents double-logging healthcheck errors
    
    app.UseSerilogRequestLogging();
    
    // ...or after serilog
    //app.MapHealthChecks("/healthz").ShortCircuit();
    
    // other middleware...
    
    // lastly (though could move auth upward if needed)
    app.UseAuthentication();
    app.UseAuthorization();
    app.MapRazorPages();
    app.MapControllers();
    

    Failed healthcheck in Development:

    [2024-12-02T08:01:29.307Z+00:00 DBG Microsoft.Extensions.Diagnostics.HealthChecks.DefaultHealthCheckService] Running health checks
    [2024-12-02T08:01:29.308Z+00:00 DBG Microsoft.Extensions.Diagnostics.HealthChecks.DefaultHealthCheckService] Running health check Default
    [2024-12-02T08:01:29.309Z+00:00 ERR Microsoft.Extensions.Diagnostics.HealthChecks.DefaultHealthCheckService] Health check Default with status Unhealthy completed after 0.0941ms with message 'null'
    [2024-12-02T08:01:29.310Z+00:00 DBG Microsoft.Extensions.Diagnostics.HealthChecks.DefaultHealthCheckService] Health check processing with combined status Unhealthy completed after 2.5307ms
    

    Failed healthcheck in Production:

    [2024-12-02T07:34:15.879Z+00:00 ERR Microsoft.Extensions.Diagnostics.HealthChecks.DefaultHealthCheckService] Health check Default with status Unhealthy completed after 0.8362ms with message 'null'
    

    Successful healthcheck in Development:

    [2024-12-02T07:28:42.124Z+00:00 DBG Microsoft.Extensions.Diagnostics.HealthChecks.DefaultHealthCheckService] Running health checks
    [2024-12-02T07:28:42.137Z+00:00 DBG Microsoft.Extensions.Diagnostics.HealthChecks.DefaultHealthCheckService] Running health check Default
    [2024-12-02T07:28:42.145Z+00:00 DBG Microsoft.Extensions.Diagnostics.HealthChecks.DefaultHealthCheckService] Health check Default with status Healthy completed after 2.2999ms with message 'null'
    [2024-12-02T07:28:42.167Z+00:00 DBG Microsoft.Extensions.Diagnostics.HealthChecks.DefaultHealthCheckService] Health check processing with combined status Healthy completed after 38.1864ms
    

    Successful healthcheck in Production: there should be no log entries.