In modern web applications, ensuring that your system is running correctly is crucial. A sudden service failure or database outage can impact users, lead to downtime, or cause data loss. Health checks in ASP.NET Core provide a simple, structured way to monitor the health of your application and its dependencies like databases, APIs, or external services.
This article explains how to implement health checks in ASP.NET Core, step by step, with practical examples and best practices for full-stack applications.
What You Will Learn
What health checks are and why they are important
How to implement built-in health checks in ASP.NET Core
How to monitor dependencies like SQL Server, Redis, or APIs
How to create custom health checks
How to expose a health check endpoint
Integration with monitoring tools
Why Health Checks Are Important
Detect failures early: Automatically detect when a service or database is down.
Improve reliability: Prevent user-facing downtime by monitoring dependencies.
Support DevOps practices: Integrate with Kubernetes, Docker, or load balancers.
Enable proactive maintenance: Send alerts to developers or administrators when services are unhealthy.
In production, health check endpoints are often polled by monitoring tools like Prometheus, Grafana, or Azure Monitor.
Part 1: Adding Health Checks in ASP.NET Core
Step 1: Install Required Packages
ASP.NET Core has built-in support for health checks. You may install additional packages for database or external service checks:
dotnet add package Microsoft.AspNetCore.Diagnostics.HealthChecks
dotnet add package AspNetCore.HealthChecks.SqlServer
Step 2: Register Health Checks
In Program.cs (ASP.NET Core 6/7+):
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
var builder = WebApplication.CreateBuilder(args);
// Register health checks
builder.Services.AddHealthChecks()
.AddSqlServer(builder.Configuration.GetConnectionString("DefaultConnection"),
name: "SQL Server",
failureStatus: Microsoft.Extensions.Diagnostics.HealthChecks.HealthStatus.Unhealthy)
.AddCheck("Custom", () =>
{
bool isHealthy = true; // Replace with custom logic
if (isHealthy)
return Microsoft.Extensions.Diagnostics.HealthChecks.HealthCheckResult.Healthy("Everything is fine");
else
return Microsoft.Extensions.Diagnostics.HealthChecks.HealthCheckResult.Unhealthy("Something is wrong");
});
var app = builder.Build();
Step 3: Configure Health Check Endpoint
app.MapHealthChecks("/health", new HealthCheckOptions
{
Predicate = _ => true, // Include all health checks
ResponseWriter = async (context, report) =>
{
context.Response.ContentType = "application/json";
var result = new
{
status = report.Status.ToString(),
checks = report.Entries.Select(e => new {
name = e.Key,
status = e.Value.Status.ToString(),
description = e.Value.Description
})
};
await context.Response.WriteAsJsonAsync(result);
}
});
Now, when you visit https://localhost:5001/health, you get a JSON response like:
{
"status": "Healthy",
"checks": [
{
"name": "SQL Server",
"status": "Healthy",
"description": "Healthy"
},
{
"name": "Custom",
"status": "Healthy",
"description": "Everything is fine"
}
]
}

Join the conversation! Your thoughts help the community grow.