Implementing Health Checks in ASP.NET WEB API
WebAPI
19 Articles
Table of Contents
What we gonna do?
In this article, let's learn about how to implement Health Checks in Web API in ASP.NET Core.
I'll introduce you to the features that ASP.NET Core offers for health checks, including how to create, register, and customize them. Lets discusses ASP.NET Core health checks, including how to configure and add health checks, create a UI to work with health checks, and secure your health check endpoints.
Why we gonna do?
Health checks are like how we humans check our health. We go to the doctor for a regular checkup to make sure everything is fine. Similarly, health checks in ASP.NET Core are used to check the health of the application. It is used to check the status of the application and its dependencies at regular intervals.
Health checks serve as endpoints that provide snapshot of application's health. They help to monitor the state of the application and its dependencies such as database, cache, external services etc.
With health checks you can easily achieve,
- Proactive Monitoring
- Automated Recovery
- Improved Reliability
The app is considered healthy if it can respond at the health endpoint URL.
Health checks in ASP.NET Core are vital for enterprise and commercial development. You need to understand what's happening on your site in order to find where problems have occurred so that in case of any failures we can be notified and take necessary actions.
How we gonna do?
Configuring Health Check Services
Configuring health checks involves two main steps in Program.cs file:
- Registering Health Check Services. This will setup Health Check Middleware.
- Configuring Health Check Endpoints. This will setup Health Check Endpoints.
Code Sample - Configuring Health Check Services
Thats the very simple and minimal configuration. Now when you run you web api and navigate to https://localhost:5001/health, you will see the health check status as shown below.
Using IHealthCheck Interface
The above setup looks neat and simple. But how do we know which part of the system is failing when we see a unhealthy status? To identify the failing part, we need to add health checks for each part of the system. We can add health check to any part of the system using IHealthCheck interface.
For example, let's add a custom health check logic to check the health of the system as shown below and register the same in the services.
Code Sample - Custom Health Check
We can now add health check for any part of the system like database, cache, external services, etc. and register them in the services. But does this seems like a lot of code? If so then you can use battle tested Health Check Library like AspNetCore.Diagnostics.HealthChecks.
This Library provides a lot of built-in health checks for various services like SQL Server, Redis, Azure Blob Storage, Postgres, MongoDB, AWS S3 etc. You can also create ui and dashboard for health checks using this library.
Adding Health Checks
To add health check to postgres database using above library, you need to install the package AspNetCore.HealthChecks.Npgsql and configure the same in the services as shown below.
Code Sample - Database Health Check
Now when you run you web api and navigate to https://localhost:5001/health, you will see the health check status as shown below.
Now that said, you can chain as many as health checks you want and configure them in the services as shown below.
Code Sample - Multiple Health Check
Creating UI and Dashboard
Having a simple string response doesn't help much. We need a UI to visualize the health check status for multiple parts of the system. To create a UI and dashboard for health checks we need to install AspNetCore.HealthChecks.UI package and install AspNetCore.HealthChecks.UI.Client package as well.
We need to store the data in a storage to show them in UI. However, for demo purpose we can use in-memory storage. To configure the same, we need to install AspNetCore.HealthChecks.UI.InMemory.Storage package and configure the same in the services as shown below.
Code Sample - Adding Health Check UI
So far so good. But what about Dashboard? Its already added. All you need to do is to navigate to https://localhost:5001/healthchecks-ui and you will see the dashboard as shown below.
If you are looking for options to customize the UI, you can do so by configuring the same with custom css shown below.
Code Sample - Health Check Dashboard Custom CSS and Registration
Securing Health Check Endpoints
By default, the health check endpoints are not secured. Anyone can access the health check endpoints. To secure the health check endpoints, you can use RequireHost, RequireCORS and RequireAuthorization middleware as shown below.
Code Sample - Securing Health Checks
That's it. You have now secured the health check endpoints.