In today’s newsletter we are going to discuss
Introduction
Different types of health checks
Level of health checks explained
How to implement them in .NET 6
How to create your custom checks
How to implement health checks for third parties
Introduction
Health checks are used to check overall health and availability of our application infrastructure. These checks are exposed by an app as HTTP endpoints.
Whenever a health check fails , we can implement some notifications e.g. email to keep us posted about issues. Health check endpoints can be configured for various real-time monitoring scenarios.
Different types of health checks
We have different types of health checks that serve different purpose
Check dependencies
Databases connections
External endpoints
Monitor memory, disk and physical server resources
Level of health checks explained
Health checks have three levels
Healthy :- Everything is working fine in our app
Unhealthy :- Some checks is being failed or throwing unhandled exception
Degraded :- Application is responding but taking more time than expected
How to implement them in .NET 6
Install this Nuget Package before moving forward
AspNetCore.HealthChecks.UI.Client
Step 1 : Configure Program.cs , app.MapHealthChecks(“/health”)
add this code after app.UseHttpsRedirection();
ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse
This line displays a proper formatted response to user , let’s see in next section
Step 2 : Register dependency of your checks (Either custom or built in )
How to create your custom checks
Step 1 : Create a class inheriting IHealthCheck
and implement its method CheckHealthAsync
Step 2 : Register your check in Program.cs
, I have registered Custom check and some dummy checks as well to demonstrate that on same fashion we can add multiple checks as well.
Let’s test it via Postman
How to implement health checks for third parties
We have bunch of libraries available that serve different purpose , if you don’t like creating custom checks you can use built in as well.
Search your desired package and implement it you have to just add the name of package and provide it configurations if applicable (like connection strings).
There are still few things in this topic to cover , like
Adding a dashboard
Defining CORS and authorization
Proper implementation of SQL custom health check
You can explore it here or if you want to me to cover those respond this email.
Whenever you’re ready , there are 3 ways I can help you
Promote yourself to 4000+ subscribers by sponsoring my Newsletter (Reach me at mwaseemzakir@gmail.com)
Become a Patron and get access to 120+ .NET Questions and Answers , I add 2-5 new questions every week
Get my FREE eBook from Gumroad that contains 30 .NET Tips (Downloaded by 2400+ and 100+ five star ratings)
Special Offers 📢
Ultimate ASP.NET Core Web API Second Edition - Premium Package
10% off with discount code: 9s6nuez