HomeDev GuideAPI Reference
Dev GuideAPI ReferenceLegal TermsDev CommunityOptimizely AcademySubmit a ticketLog In
Dev Guide

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:

  1. Implement your health check logic by implementing IHealthCheck or EPiServer.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 implement IHealthCheck or EPiServer.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 use EPiServer.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 using IHealthCheck

    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."));
      }
    }
    
  2. Register the SampleHealthCheck into services with your name, tags, and so on, by using IHealthChecksBuilder, a builder used to register health checks. Create an extension method to register the SampleHealthCheck service, which is a part of the IHealthChecksBuilder.

    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);
      }
    }
    
  3. Call AddSampleHealthCheck from ConfigureServices (IServiceCollection services) in start-up class.

    services.AddHealthChecks().AddSampleHealthCheck ();