Building Resilient Applications with .NET Core Health Checks Using AspNetCore.Diagnostics.HealthChecks
Reliable and efficient monitoring is crucial for managing the health of applications. Especially in a microservices architecture, you want to know at all times if any service is down or experiencing problems. In this blog post, we’ll be looking at how we can use .NET Core Health Checks, along with a fantastic library, AspNetCore.Diagnostics.HealthChecks
, to build reliable, robust applications.
Introduction to .NET Core Health Checks
Introduced in .NET Core 2.2, Health Checks provide a standardized way for applications to expose their state. The purpose is simple: when you hit a predefined endpoint (usually /health
), it tells you whether the application is healthy, degraded, or unhealthy. You can also extend it to check the health of external resources, like databases or APIs, which the application depends on.
The system provides three statuses:
HealthStatus.Healthy
: Everything is up and running.HealthStatus.Degraded
: The service is running but with some issues.HealthStatus.Unhealthy
: The service is down.
While the native health checks are quite useful, they’re also pretty basic. They don’t provide detailed information about what’s wrong, nor do they offer any type of dashboard or storage for the health check history. This is where AspNetCore.Diagnostics.HealthChecks
comes into play.
The AspNetCore.Diagnostics.HealthChecks Library
The AspNetCore.Diagnostics.HealthChecks is an open-source project hosted on GitHub (https://github.com/Xabaril/AspNetCore.Diagnostics.HealthChecks) and provides extended functionality to the built-in health checks in .NET Core. It includes several Health Checks for common configurations, like SqlServer, MySql, Postgress, Redis, Elasticsearch, and many more. It also provides two additional crucial features: Health Check UI and Health Checks storage.
Health Checks UI is a visual interface where you can easily see the status of your services. It is super handy for quickly identifying problems and knowing which part of your system is affected.
Health Checks Storage allows you to keep a record of health check status history. This can be beneficial for understanding how your system behaves over time and identifying recurring issues.
Integrating AspNetCore.Diagnostics.HealthChecks
Now, let’s see how to integrate the AspNetCore.Diagnostics.HealthChecks library into our .NET Core applications.
Step 1: Install the necessary packages
First, install the required NuGet packages. For instance, if you want to add health checks for a SQL Server database and use a UI for visualizing the results, you’ll need to add the following packages:
dotnet add package AspNetCore.HealthChecks.SqlServer dotnet add package AspNetCore.HealthChecks.UI
Step 2: Configure Health Checks
Next, you need to configure the health checks in the Startup.cs
file:
public void ConfigureServices(IServiceCollection services) { services.AddHealthChecks() .AddSqlServer( connectionString: Configuration.GetConnectionString("DefaultConnection"), name: "Database check", failureStatus: HealthStatus.Unhealthy, tags: new string[] { "db", "sql", "sqlServer" }); services.AddHealthChecksUI().AddInMemoryStorage(); } public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { app.UseEndpoints(endpoints => { endpoints.MapHealthChecks("/health"); endpoints.MapHealthChecksUI(); }); }
In the ConfigureServices
method, we add SQL Server health check and specify the UI with InMemoryStorage. In the Configure
method, we define two endpoints for the health checks and the health checks UI.
Step 3: Monitoring Health Checks
Once the application is running, you can check the service’s health status by navigating to http://yourapp/health
. You’ll see a status response. For a more visual display, navigate to http://yourapp/healthchecks-ui
.
Wrapping up
ASP.NET Core’s built-in health checks combined with AspNetCore.Diagnostics.HealthChecks provide a comprehensive and extensible health check system. The library enhances the built-in features with more specific checks, a UI dashboard, and storage providers, making it easier to monitor your applications and services.
With this set up, you can ensure you’re immediately aware of any issues affecting your application and can respond quickly to maintain an efficient, reliable service for your users. And, with the source code freely available, you can also contribute to the project and help make .NET Core Health Checks even better.
So, go ahead and give your applications a health check-up! They (and their users) will thank you for it.
Resources:
https://github.com/Xabaril/AspNetCore.Diagnostics.HealthChecks