Implementing Health Checks in ASP.NET Core

Ensuring your services are healthy has never been easier

Christopher Laine
Jun 29, 2019 · 7 min read
Image for post
Image for post
Photo by Jair Lázaro on Unsplash

When partial failure attacks

And then there’s the times when things go pear-shaped.

Health Checks to the rescue

ASP.NET Core Health Checks

install-package Microsoft.AspNetCore.Diagnostics.HealthChecks
public void ConfigureServices(IServiceCollection services)
{
services.AddHealthChecks();
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
//other code
app.UseHealthChecks("/health");
//other code
}
REQUEST:
GET /health HTTP/1.1
Host: localhost:54411
Accept: */*
Cache-Control: no-cache
Host: localhost:54411
accept-encoding: gzip, deflate
Connection: keep-alive
cache-control: no-cache
RESPONSE:
200 OK
Healthy
public class ApiHealthCheck : IHealthCheck
{
public Task<HealthCheckResult> CheckHealthAsync(
HealthCheckContext context,
CancellationToken cancellationToken = new CancellationToken())
{
//TODO: Implement your own healthcheck logic here
var isHealthy = true;
if(isHealthy)
{
return Task.FromResult(HealthCheckResult.Healthy("I am one healthy microservice API"));
}

return Task.FromResult(HealthCheckResult.Unhealthy("I am the sad, unhealthy microservice API"));
}
}
public void ConfigureServices(IServiceCollection services)
{
services.AddHealthChecks()
.AddCheck<ApiHealthCheck>("api");

Customising HealthCheck output

private static Task WriteHealthCheckResponse(HttpContext httpContext,
HealthReport result)
{
httpContext.Response.ContentType = “application/json”; var json = new JObject(
new JProperty(“status”, result.Status.ToString()),
new JProperty(“results”, new JObject(result.Entries.Select(pair =>
new JProperty(pair.Key, new JObject(
new JProperty(“status”, pair.Value.Status.ToString()),
new JProperty(“description”, pair.Value.Description),
new JProperty(“data”, new JObject(pair.Value.Data.Select(
p => new JProperty(p.Key, p.Value))))))))));
return httpContext.Response.WriteAsync(
json.ToString(Formatting.Indented));
}
app.UseHealthChecks(“/health”, new HealthCheckOptions()
{
//that's to the method you created
ResponseWriter = WriteHealthCheckResponse
});
{
"status": "Healthy",
"results": {
"api": {
"status": "Healthy",
"description": "I am one healthy microservice API",
"data": {}
}
}
}

Chaining Health Checks

public class SecondaryHealthCheck : IHealthCheck
{
public Task<HealthCheckResult> CheckHealthAsync(
HealthCheckContext context,
CancellationToken cancellationToken = new CancellationToken())
{
return Task.FromResult(HealthCheckResult.Degraded("Not feeling so well, boss!"));
}
}
services.AddHealthChecks()
.AddCheck<ApiHealthCheck>("api")
.AddCheck<SecondaryHealthCheck>("secondary");
{
"status": "Degraded",
"results": {
"api": {
"status": "Healthy",
"description": "I am one healthy microservice API",
"data": {}
},
"secondary": {
"status": "Degraded",
"description": "Not feeling so well, boss!",
"data": {}
}
}
}

Pre-canned Health Checks

Conclusion

IT Dead Inside

IT is a cesspool, but its home

Christopher Laine

Written by

Author, programmer, would-be philosopher. Author of Screens https://christopherlaine.net/screens

IT Dead Inside

IT is a cesspool, but its home

Christopher Laine

Written by

Author, programmer, would-be philosopher. Author of Screens https://christopherlaine.net/screens

IT Dead Inside

IT is a cesspool, but its home

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store