Health Checks & Monitoring in ASP.NET Core
π‘ Concept Name
Health Checks & Monitoring in ASP.NET Core
π Quick Intro
Health checks in ASP.NET Core help expose the health of your application via endpoints. They're essential for uptime, observability, and diagnostics. Commonly used in cloud-native and microservices setups.
π§ Analogy / Short Story
Imagine your app as a patient in a hospital. Health checks are like periodic medical tests (heartbeat, blood pressure) to ensure vital signs are normal. If somethingβs off, the monitoring system alerts the doctor (devs or ops) instantly.
π§ Technical Explanation
ASP.NET Core provides `Microsoft.AspNetCore.Diagnostics.HealthChecks` to add health endpoints.
You register checks in `Program.cs` using `AddHealthChecks()` and map them with `MapHealthChecks("/health")`.
You can check memory, database connection, custom services, and even use tags for readiness/liveness probes in Kubernetes.
π― Purpose & Use Case
- β Expose liveness/readiness status for Kubernetes probes
- β Monitor database, cache, or external services availability
- β Integrate with Prometheus, Grafana, Azure Monitor
- β Gracefully drain unhealthy instances
- β Enable proactive maintenance and alerting
π» Real Code Example
// Program.cs (ASP.NET Core 6+)
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddHealthChecks()
.AddSqlServer(builder.Configuration.GetConnectionString("DefaultConnection"));
var app = builder.Build();
app.MapHealthChecks("/health");
app.Run();

β Interview Q&A
Q1: What namespace is used for health checks?
A: Microsoft.AspNetCore.Diagnostics.HealthChecks
Q2: What is the default path to expose health?
A: It's usually `/health` or `/healthz`
Q3: Can I create custom health checks?
A: Yes, by implementing `IHealthCheck`.
Q4: What's the purpose of tags in health checks?
A: To filter checks for specific probes like readiness or liveness.
Q5: Are health checks async?
A: Yes, all checks are `Task`-based for async support.
Q6: Can we return custom responses?
A: Yes, by providing a custom `HealthCheckOptions.ResponseWriter`.
Q7: How to monitor Redis health?
A: Use `.AddRedis()` in the health check registration.
Q8: Is it possible to secure health endpoints?
A: Yes, with authentication/authorization middleware.
Q9: What HTTP status code means unhealthy?
A: Typically 503 (Service Unavailable).
Q10: Can health checks help with autoscaling?
A: Yes, they indicate instance readiness/unhealthiness for orchestration systems.
π MCQs
Q1: What is the primary use of health checks?
- A. Load testing
- B. UI Testing
- C. Monitor app health
- D. Logging requests
Q2: Which method adds health check services?
- A. AddHealthChecks()
- B. AddMonitoring()
- C. UseHealthStatus()
- D. ConfigureHealth()
π‘ Bonus Insight
Tag-based filtering in ASP.NET Core health checks lets you categorize checks for readiness vs liveness β a critical feature when deploying in Kubernetes or Docker Swarm.
π PDF Download
Need a handy summary for your notes? Download this topic as a PDF!