👉🏼 Click here to Join I ❤️ .NET WhatsApp Channel to get 🔔 notified about new articles and other updates.
Implementing Health Checks in ASP.NET WEB API

Implementing Health Checks in ASP.NET WEB API

WebAPI

19 Articles

Improve

Table of Contents

  1. What we gonna do?
  2. Why we gonna do?
  3. How we gonna do?
  4. Summary

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:

  1. Registering Health Check Services. This will setup Health Check Middleware.
  2. 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.

Simple Health Check Response

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.

Degraded Health Check Response

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

Health UI Response

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.

Health Check Dashboard

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

Health Check Dashboard Custom UI

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.

Summary

In this we learnt about what is health check and why it is important. We also learnt how to configure health checks in ASP.NET Core Web API. We also learnt how to add custom health checks using IHealthCheck and use library to add health checks for various services. We also learnt how to create UI and dashboard for health checks and how to customise the UI and secure the health check endpoints. Now you are ready to implement health checks in your ASP.NET Core Web API.
👉🏼 Click here to Join I ❤️ .NET WhatsApp Channel to get 🔔 notified about new articles and other updates.
  • Webapi
  • Health Check
  • Alive