In this article, I will integrate ASP.NET health check to the ASP.NET Boilerplate project.
What is ASP.NET Health Check
ASP.NET Core offers Health Check Middleware and libraries for reporting the health of app infrastructure components. It allows you to check the health of the application.
There are dozens of libraries you can use with health checks. And you can also create your own health checks.
Let’s start.
- Download your ASP.NET Boilerplate application.
Go to https://aspnetboilerplate.com/Templates and download your .NET Core application and make the first setup (update DB, etc.)
(In this article I will use Multi-Page Web Application)
See: https://aspnetboilerplate.com/Pages/Documents for more information.
- Add
Microsoft.AspNetCore.Diagnostics.HealthChecks
NuGet package to your*.Web.Mvc
project. - Open
*.Web.Mvc
project’sStartup.cs
file and add health checks middleware as seen below.
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
...
services.AddHealthChecks();
...
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
...
app.UseHealthChecks("/health");//your request URL will be health
...
}
}
Now it returns healthy string to show us the system is up and healthy.
Add Health Check UI
There are dozens of libraries that you can use with health check.
Let’s use AspNetCore.HealthChecks.UI
AspNetCore.HealthChecks.UI is a library that gives you a nice looking user interface. Every health check that you add will be automatically added to the user interface .
- Add
AspNetCore.HealthChecks.UI
NuGet package to your*.Web.Mvc
project - Open
*.Web.Mvc
project’sStartup.cs
file and change it as shown below.
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
...
services.AddHealthChecks();
services.AddHealthChecksUI();
...
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
...
app.UseHealthChecks("/health", new HealthCheckOptions()
{
Predicate = _ => true,
ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse
});
app.UseHealthChecksUI();
...
}
}
- Add your UI settings to
*.Web.Mvc
project’sappsettings.json
file.
"HealthChecksUI": {
"HealthChecks": [
{
"Name": "HealthCheckExample",
"Uri": "http://localhost:62114/health"
}
],
"EvaluationTimeOnSeconds": 10,
"MinimumSecondsBetweenFailureNotifications": 60
}
After that, the response from the URL /health
will be like below.
And you will have health checks ui.
http://localhost:62114/healthchecks-ui
Create Your Own Health Checks
To check something that you want you can use existing libraries or you can create your own health checker classes inherited from IHealthCheck
. To use an existing library see their documentation. I will create a custom health checker.
- Add
Microsoft.Extensions.Diagnostics.HealthChecks.Abstraction
NuGet package to your*.Application
project. - Create a folder named
HealthChecks
in*.Application
project and create your custom health check service class in it. (I will create DB context checker namedHealthCheckExampleDbContextCheck
)
- Instead of adding all of your health check in
Startup.cs
we will create health check builder to*.Web.Core
. CreateHealthChecks
folder in the*.Web.Core
project. Create a class namedHealthCheckExampleHealthCheckBuilder
as shown below. - Use your new builder in
Startup.cs
Finally you can see the health check status as seen below:
You can check the health status of the following list or any other 3rd parties.
- Sql Server, MySql, Oracle …
- EventStore
- RabbitMQ
- Elasticsearch
- Redis
- …
See https://github.com/Xabaril/AspNetCore.Diagnostics.HealthChecks for more.
You can add more and more to your health check builder and check all dependencies of your application. Check your DB, micro-services, Redis, Azure Storage, Identity Server, Uris, local storage, etc… whatever you need.
See also https://docs.docker.com/engine/reference/builder/#healthcheck
Example project GitHub URL:
https://github.com/aspnetboilerplate/aspnetboilerplate-samples/tree/master/HealthCheck/aspnet-core