Posted by Anuraj on Monday, July 6, 2020 Reading time :3 minutes
This blog post is about how to configure health check in ASP.NET Core. Health monitoring an important aspect in case of Micro services. It will help orchestrators to monitor and manage containers.
ASP.NET Core offers health checks out of the box in the .NET Core Framework. In this post first I will be implementing health check for ASP.NET Core API application. Next we will integrate health check for SQL Server or Database using the DbContext and finally how to implement health check for a custom service.
To implement health check, you need to add reference of
Microsoft.AspNetCore.Diagnostics.HealthChecks package to the application.
Next in the
ConfigureServices method add the Health check middleware.
Next in the
Configure method, add the following code to enable routing to health endpoint.
Now if you run the application and try to access the
/health endpoint you will be able to see the response like healthy with a status code 200.
In some situations, like you’re using Open API to document your endpoints, you might need to use a health endpoint from controller. ASP.NET Core provides a
HealthCheckService class which help us to implement it from controller, like this.
Now if you try to access the
/health endpoint you will see the same result.
Next we will implement the database health monitoring.
Similar to application, you can enable the database health monitoring as well. To do this, you need to add the
Microsoft.Extensions.Diagnostics.HealthChecks.EntityFrameworkCore package to the project.
Next modify the code to include the database context as well in the health check like this.
Here is the response from Health endpoint after adding the DbContext health check.
In this section, you learned how to configure health check for Database. Next section we will learn about creating health check service for your application.
If you’re using a service which doesn’t supported by ASP.NET Core health check providers out of the box - you can implement your own. For this purpose Microsoft is providing one interface -
In this example, I am checking for an API service is running or not.
And you can attach it to the health middleware like this.
Now if you run the app and try to access the health endpoint you will be able to see something like this.
healthy() method result you can include the response from the API so that it will be more useful for the developer who is monitoring the app. You can even create an extension method which helps to add the ApiHealth check class to the healthcheck middleware like this.
And can modify the
ConfigureServices code like this.
So using this method you can extend health monitoring support third party applications and services. In this blog post you learned about configuring and implementing ASP.NET Core health checks. You can find the source code for this blog post in GitHub.
Happy Programming :)
What do you think? I would like to hear your thoughts, suggestions, and questions in the comments section below.