Demirmusa
Demirmusa
Jul 25 · 3 min read

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’s Startup.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’s Startup.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’s appsettings.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.

http://localhost:62114/health

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 named HealthCheckExampleDbContextCheck )
  • Instead of adding all of your health check in Startup.cs we will create health check builder to *.Web.Core. Create HealthChecks folder in the *.Web.Core project. Create a class named HealthCheckExampleHealthCheckBuilderas shown below.
  • Use your new builder in Startup.cs

Finaly 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

Volosoft

Volosoft

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade