Health Check with Asp.Net Core – Part 1

Is very important to know if your application is healthy, so you will check to be sure your site is running but is that enough? Maybe your database is responding slowly, maybe your logging has crashed or maybe your server is about to run out of hard drive space.

How would you know that?

You can use health check libraries and asp.net core middleware to verify and report on the health and stability of your application, checking real-time information on the state of your containers and microservices. Furthermore, is possible to monitor physical server resources like the use of memory and disk usage, test app’s dependencies, such as external services endpoint, database, logging, among other functions as you can see below.

Basic use of health check:

Implement health check is easier, it will take a few steps to start to use:

  1. This functionality was released in asp.net core 2.2, to start to use add the nuget package Microsoft.Extensions.Diagnostics.HealthChecks
  2. Add services.AddHealthChecks(); inside the method ConfigureServices on the Startup.Cs
  3. Add endpoints.MapHealthChecks(“healthcheck”); on Startup.cs class. Note you can inform any endpoint name here, I choose healthcheck, but you can choose another name.
  4. Now you will run your application, accessing the endpoint localhost/healthcheck, then you can see if your application is healthy or unhealthy automatically with plain text.

Showing results with more details:

Perhaps, do you need to show more details on your results. It is possible with HealthCheckOptions where you can build a custom output writer and use health check options to inject it into the health check feature.

Access Github to read the complete code:

https://github.com/tatianilima/health-check-basic