Health checks — monitor app and dependency health.
Setup:
1builder.Services.AddHealthChecks()2 .AddSqlServer(connectionString, name: "sql")3 .AddRedis(redisConnection, name: "redis")4 .AddCheck<CustomHealthCheck>("custom");
Custom health check:
1public class CustomHealthCheck : IHealthCheck2{3 public async Task<HealthCheckResult> CheckHealthAsync(4 HealthCheckContext context,5 CancellationToken ct = default)6 {7 try8 {9 // Check something10 return HealthCheckResult.Healthy("OK");11 }12 catch (Exception ex)13 {14 return HealthCheckResult.Unhealthy(ex.Message);15 }16 }17}
Endpoint:
1app.MapHealthChecks("/health");2app.MapHealthChecks("/health/ready", new HealthCheckOptions3{4 Predicate = check => check.Tags.Contains("ready")5});
Response format: JSON with status and checks.