Customize health checks
ASP.NET Core offers health checks middleware and libraries for reporting the health of app infrastructure components. Health checks are created by implementing the IHealthCheck
interface.
The Optimizely health check is based on Health checks in ASP.NET Core in Microsoft Learn and extends IHealthCheck
by EPiServer.Cms.HealthCheck.IsolatedHealthCheck
. If you need to extend the health check with your custom logic, use the following steps:
-
Implement your health check logic by implementing
IHealthCheck
orEPiServer.Cms.HealthCheck.IsolatedHealthCheck
.IHealthCheck
represents a health check, which can be used to check the status of a component in the application, such as a backend service, database, or some internal state.EPiServer.Cms.HealthCheck.IsolatedHealthCheck
is an abstract helper class that provides access to the service collection. The custom health check implementation can implementIHealthCheck
orEPiServer.Cms.HealthCheck.IsolatedHealthCheck
. If you need to access services that are not registered as scoped but should be used as scoped, then you need to useEPiServer.Cms.HealthCheck.IsolatedHealthCheck
.public class SampleHealthCheck: IHealthCheck { public Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default) { var isHealthy = true; // ... add your logic here to detect if the application is in healthy state. if (isHealthy) { return Task.FromResult(HealthCheckResult.Healthy("A healthy result.")); } return Task.FromResult(new HealthCheckResult(context.Registration.FailureStatus, "An unhealthy result.")); } }
SampleHealthCheck
by usingIHealthCheck
public class SampleHealthCheck: IsolatedHealthCheck { public SampleHealthCheck(IBackgroundContextFactory backgroundContextFactory): base(backgroundContextFactory) {} public override Task<HealthCheckResult> CheckHealthAsync(IsolatedHealthCheckContext context, CancellationToken cancellationToken = default) { var isHealthy = true; // ... add your logic here to detect if the application is in healthy state. // var yourDependencyServiceInstance = context.Services.GetInstance<YourDependencyService>(); if (isHealthy) { return Task.FromResult(HealthCheckResult.Healthy("A healthy result.")); } return Task.FromResult(new HealthCheckResult(context.Registration.FailureStatus, "An unhealthy result.")); } }
-
Register the
SampleHealthCheck
into services with your name, tags, and so on, by usingIHealthChecksBuilder
, a builder used to register health checks. Create an extension method to register theSampleHealthCheck
service, which is a part of theIHealthChecksBuilder
.public static class HeathCheckExtensions { public static IHealthChecksBuilder AddSampleHealthCheck(this IHealthChecksBuilder builder, string name = "My-health-check", HealthStatus status = HealthStatus.Unhealthy, IEnumerable<string> tags = { "your tags" }) { // register SampleHealthCheck in container as the service should be registered like singleton, per request etc. This sample shown how you can register as singleton. Builder.Services.AddSingleton<SampleHealthCheck>(); return builder.AddCheck<SampleHealthCheck>(name, new HealthStatus ? (status), tags); } }
-
Call
AddSampleHealthCheck
fromConfigureServices
(IServiceCollection
services) in start-up class.services.AddHealthChecks().AddSampleHealthCheck ();
Updated 7 months ago