Listen to this Post
Health Checks in ASP.NET are a way to assess the health of an application and its dependencies. They monitor the status and performance of various components, such as databases, external services, or internal services, providing insights into whether an application is running as expected or if issues need addressing.
Read the full article here: Health Checks in ASP.NET
You Should Know:
1. Adding Health Checks to Your App
To enable Health Checks in an ASP.NET Core application, add the following to your Startup.cs:
public void ConfigureServices(IServiceCollection services)
{
services.AddHealthChecks();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseEndpoints(endpoints =>
{
endpoints.MapHealthChecks("/health");
});
}
2. Creating Custom Health Checks
Extend `IHealthCheck` to create custom health checks:
public class CustomDbHealthCheck : IHealthCheck
{
public async Task<HealthCheckResult> CheckHealthAsync(
HealthCheckContext context,
CancellationToken cancellationToken = default)
{
try
{
// Check database connectivity
return HealthCheckResult.Healthy("Database is OK!");
}
catch (Exception ex)
{
return HealthCheckResult.Unhealthy("Database failure!", ex);
}
}
}
Register it in `ConfigureServices`:
services.AddHealthChecks()
.AddCheck<CustomDbHealthCheck>("database_health_check");
3. Using Built-in Health Checks
ASP.NET provides built-in checks for SQL Server, Redis, and more:
services.AddHealthChecks()
.AddSqlServer(Configuration.GetConnectionString("DefaultConnection"))
.AddRedis("redis_connection_string");
4. Enabling Health Checks UI
Install the `AspNetCore.HealthChecks.UI` package and configure:
services.AddHealthChecksUI()
.AddInMemoryStorage();
app.UseHealthChecks("/health-ui", new HealthCheckOptions
{
Predicate = _ => true,
ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse
});
5. Writing Custom Output
Modify the health response format:
app.UseHealthChecks("/health", new HealthCheckOptions
{
ResponseWriter = async (context, report) =>
{
var result = JsonConvert.SerializeObject(new
{
status = report.Status.ToString(),
checks = report.Entries.Select(e => new
{
name = e.Key,
status = e.Value.Status.ToString()
})
});
context.Response.ContentType = "application/json";
await context.Response.WriteAsync(result);
}
});
What Undercode Say:
Health Checks are essential for maintaining robust applications. Beyond ASP.NET, similar concepts exist in Linux (systemctl status), Windows (Get-Service), and cloud platforms. For deeper monitoring, integrate with tools like Prometheus or Azure Monitor.
Linux Commands for Health Monitoring:
systemctl status nginx Check service status htop Monitor system resources netstat -tuln Check open ports df -h Disk space health
Windows Commands:
Get-Service -Name "MSSQLSERVER" Check SQL Server status Test-NetConnection -ComputerName google.com -Port 80 Network check Get-WinEvent -LogName "Application" -MaxEvents 10 Check recent errors
Expected Output:
A well-monitored application with automated health checks, ensuring high availability and quick issue resolution.
Read more about ASP.NET Health Checks
References:
Reported By: Djokic Stefan – Hackers Feeds
Extra Hub: Undercode MoN
Basic Verification: Pass ✅



