HealthCheck in .NET CORE

Eran Hadad
2 min readNov 8, 2020

Introduction

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 custom health checks.

Packages

After creating the .NET Core project, You will need to install a number of packages.

AspNetCore.HealthChecks.UI
AspNetCore.HealthChecks.UI.Client
AspNetCore.HealthChecks.UI.InMemory.Storage

To integrate HealthChecks.UI in your project you just need to add the HealthChecks.UI services and middleware available in the package: AspNetCore.HealthChecks.UI

using HealthChecks.UI.Core;
using HealthChecks.UI.InMemory.Storage;

public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services
.AddHealthChecksUI()
.AddInMemoryStorage()

}

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app
.UseRouting()
.UseEndpoints(config =>
{
config.MapHealthChecksUI();
});
}
}

These repositories offer a wide collection of ASP.NET Core Health Check packages for widely used services and platforms.

Extensions

There are lots of extensions that already exist and there is no need to implement custom check, for example —

AspNetCore.HealthChecks.MongoDb
AspNetCore.HealthChecks.Network
AspNetCore.HealthChecks.System
AspNetCore.HealthChecks.SqLite
AspNetCore.HealthChecks.SqlServer
AspNetCore.HealthChecks.Kubernetes
AspNetCore.HealthChecks.Hangfire
etc...

How to use

In the ConfigureServices [Startup.cs] you can use the extension methods above, for example —

services.AddHealthChecks()
.AddMongoDb(mongodbConnectionString: Configuration.GetSection("DbSettings:ConnectionString").Value,
name: "mongo",
failureStatus: HealthStatus.Unhealthy);

Health Status

Degraded — This indicates that the health check determined that the component was in a degraded state.

Healthy — This indicates that the health check determined that the component was healthy.

Unhealthy — This indicates that the health check determined that the component was unhealthy, or an unhandled exception was thrown while executing the health check.

Custom Health Checkers

you can also inject your own custom health check

services.AddHealthChecks().AddCheck<MyCustomHealthCheck>("My custuom health check", failureStatus: HealthStatus.Unhealthy, new[] { "folders" });

your MyCustomHealthCheck.cs must implement IHealthCheck and implement CheckHealthAsync —

public Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default)

To use the UI go to the ConfigureServices in startup.cs and extended the function services.AddHealthChecksUI (Optional — use WEBHOOK to get notify for errors).

services.AddHealthChecksUI(options =>
{
options.MaximumHistoryEntriesPerEndpoint(50);
options.AddWebhookNotification("webhook1", uri: "/api/HealthCheck",
payload: "{ \"message\": \"Webhook report for [[LIVENESS]]: [[FAILURE]] - Description: [[DESCRIPTIONS]]\"}",
restorePayload: "{ \"message\": \"[[LIVENESS]] is back to life\"}";
}).AddInMemoryStorage();

And in the Configure —

app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
endpoints.MapHealthChecksUI();
endpoints.MapHealthChecks("/health", new HealthCheckOptions
{
ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse
});

});

Change the UI style.css

You can also change the UI style with your custom style css file —

app.UseEndpoints(endpoints =>{endpoints.ConfigureHealthCheckRoutes(System.IO.Path.Combine(System.IO.Directory.GetCurrentDirectory(),"Resources", "mystyle.css"));endpoints.MapControllers();});

and this is mystyle.css

:root {--primaryColor: #30BBDE;--secondaryColor: #B30F0F;--bgMenuActive: #30BBDE;--bgButton: #30BBDE;--logoImageUrl: url('https://whitebook.eurhonet.eu/wp-content/uploads/2018/10/monitoring.png');--bgAside: var(--primaryColor);}

Good Luck

Eran Hadad

--

--