Skip to content

Latest commit

 

History

History
108 lines (86 loc) · 2.9 KB

README.md

File metadata and controls

108 lines (86 loc) · 2.9 KB

Atc.Rest.HealthChecks

This library contains features related to ASP-NET HealthChecks.

Requirements

Code documentation

References

References extended

Examples

The Startup.cs class in an API

using Microsoft.Extensions.Diagnostics.HealthChecks;

public class Startup
{
    private readonly JsonSerializerOptions jsonSerializerOptions;

    public Startup()
    {
        jsonSerializerOptions = Atc.Serialization.JsonSerializerOptionsFactory.Create();
    }

    public void ConfigureServices(IServiceCollection services)
    {
        services
            .AddHealthChecks()
                .AddCheck<MyHealthCheck>(
                    name: "MyHealthCheck",
                    failureStatus: HealthStatus.Unhealthy,
                    timeout: TimeSpan.FromSeconds(30));
    }

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        app.UseEndpoints(endpoints =>
        {
            endpoints.MapHealthChecks(
                "/health",
                HealthCheckOptionsFactory.CreateJson(
                    "ApplicationName.Api",
                    jsonSerializerOptions));
        });
    }
}

The MyHealthCheck class

public class MyHealthCheck : IHealthCheck
{
    public Task<HealthCheckResult> CheckHealthAsync(
        HealthCheckContext context,
        CancellationToken cancellationToken = default)
    {
        var data = new List<ResourceHealthCheck>();

        CheckSomeServices(data);

        if (data.All(x => x.Status == HealthStatus.Healthy))
        {
            return Task.FromResult(
                HealthCheckResult.Healthy(
                    "ApplicationName is healthy.",
                    data: data.ToIReadOnlyDictionary()));
        }

        return Task.FromResult(
            HealthCheckResult.Unhealthy(
                "ApplicationName is unhealthy.",
                exception: null,
                data: data.ToIReadOnlyDictionary()));
    }

    private void CheckSomeServices(
        ICollection<ResourceHealthCheck> data)
    {
        var sw = Stopwatch.StartNew();

        // Perform first HealthCheck and add a healthCheck to the data dictionary
        data.Add(
            new ResourceHealthCheck(
                "Service1",
                HealthStatus.Healthy,
                "Service1 is running.",
                sw.Elapsed));

        // Perform second HealthCheck and add a healthCheck to the data dictionary
        sw.Reset();
        data.Add(
            new ResourceHealthCheck(
                "Service2",
                HealthStatus.Unhealthy,
                "Service2 is not running.",
                sw.Elapsed));

        sw.Stop();
    }
}