0

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?

1 Answer 1

0

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.