1 min read

Add health checks to a .NET API


Health checks for production

Register health checks:

Program.cs
builder.Services.AddHealthChecks()
    .AddCheck("self", () => HealthCheckResult.Healthy())
    .AddNpgSql(builder.Configuration.GetConnectionString("Db")!);

Map the endpoints:

Program.cs
app.MapHealthChecks("/health/live", new HealthCheckOptions
{
    Predicate = check => check.Name == "self"
});
 
app.MapHealthChecks("/health/ready");

Use /health/live for Kubernetes liveness probes and /health/ready for readiness probes. The readiness probe includes the database check so traffic isn’t routed until the DB is reachable.