HealthCheckPlus 1.0.5

There is a newer version of this package available.
See the version list below for details.
dotnet add package HealthCheckPlus --version 1.0.5                
NuGet\Install-Package HealthCheckPlus -Version 1.0.5                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="HealthCheckPlus" Version="1.0.5" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add HealthCheckPlus --version 1.0.5                
#r "nuget: HealthCheckPlus, 1.0.5"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install HealthCheckPlus as a Cake Addin
#addin nuget:?package=HealthCheckPlus&version=1.0.5

// Install HealthCheckPlus as a Cake Tool
#tool nuget:?package=HealthCheckPlus&version=1.0.5                

Welcome to HealthCheckPlus

HealthCheck with individual delay and interval and interval policy for unhealthy/degraded status.

HealthCheckPlus was developed in c# with the netstandard2.1, .Net6, .Net7 and .Net8 target frameworks.

V1.0.5

  • Added parameter 'Delay' on AddHealthChecks<T> : Initial delay applied after the application starts. The default value is 5 seconds.The min.value is 1 second.
  • Added parameter 'Period' on AddHealthChecks<T> : Period of HealthCheckPublisher execution. The default value is 1 seconds. The min.value is 500 milesecond.

What's new V1.0.4

  • First Release G.A

Features

  • Delay and interval for HealthCheckPublisher
  • Delay and interval for each HealthCheck
  • Interval policy for unhealthy status for each HealthCheck
  • Interval policy for degraded status for each HealthCheck
  • Endpoints returns the latest internal status protecting your application.
  • The parameter period for each health check acts as a circuit breaker when using it in your business logic, improving application responsiveness in high request rate scenarios and protecting your infrastructure.
  • Change to unhealthy/degraded any HealthCheck by forcing check by interval policy
  • Register an external health check (packet import) and associate delay, interval and individual policy rules.
  • Optional action to write log.
  • Optional parameter for log category name.
  • Simple and clear fluent syntax

Usage

//Create enum with all HealthCheck
public enum MyEnum
{
    HcTeste1,
    HcTeste2,
    Redis
}
//At Statup / Program
builder.Services
    //Add HealthCheckPlus
    .AddHealthChecks<MyEnum>("AppHealthCheck", (deps) =>
        //custom result status 
        {
            if (deps.TryGetNotHealthy(out _))
            {
                return HealthStatus.Degraded;
            }
            return HealthStatus.Healthy;
        },
        //category log
        "HealthCheckPlusDemo",
        //action for log    
        (log, result) =>
    {
        switch (result.Status)
        {
            case HealthStatus.Unhealthy:
                log.LogError($"{result.Name} : {result.Description} : {result.Status} : {result.ElapsedTime} : {result.Date}");
                break;
            case HealthStatus.Degraded:
                log.LogWarning($"{result.Name} : {result.Description} : {result.Status} : {result.ElapsedTime} : {result.Date}");
                break;
            case HealthStatus.Healthy:
                log.LogInformation($"{result.Name} : {result.Description} : {result.Status} : {result.ElapsedTime} : {result.Date}");
                break;
            default:
                break;
        }
    })
    //your custom HC    
    .AddCheckPlus<MyEnum, HcTeste1>(MyEnum.HcTest1, TimeSpan.FromSeconds(20), TimeSpan.FromSeconds(30))
    //your custom HC    
    .AddCheckPlus<MyEnum, HcTeste2>(MyEnum.HcTest2, TimeSpan.FromSeconds(10), TimeSpan.FromSeconds(40), failureStatus: HealthStatus.Degraded)
    //external HC 
    .AddRedis("connection string", "Myredis")
    //register external HC 
    .AddCheckRegistered(MyEnum.Redis, "MyRedis", TimeSpan.FromSeconds(5), TimeSpan.FromSeconds(60))
    //policy for Unhealthy
    .AddUnhealthyPolicy(MyEnum.HcTest1, TimeSpan.FromSeconds(2))
    //policy for Degraded
    .AddDegradedPolicy(MyEnum.HcTest2, TimeSpan.FromSeconds(3))
    //policy for Unhealthy
    .AddUnhealthyPolicy(MyEnum.Redis, TimeSpan.FromSeconds(5));
//At Statup / Program
//Endpoints HC
app.UseHealthChecksPlus("/health/live", HttpStatusCode.OK)
   .UseHealthChecksPlusStatus("/health/ready", HttpStatusCode.OK);
//At Statup / Program
//middler pipeline
_ = app.Use(async (context, next) =>
{
    if (_stateHealthChecksPlus.StatusApp.Status == HealthStatus.Unhealthy)
    {
        var msg = JsonSerializer.Serialize(new { Error = "App Unhealthy" });
        context.Response.ContentType = "application/json";
        context.Response.ContentLength = msg.Length;
        context.Response.StatusCode = 500;
        await context.Response.WriteAsync(msg);
        await context.Response.CompleteAsync();
        return;
    }
    await next();
});
//Create HealthCheck class inheriting from BaseHealthCheckPlus(IHealthCheck)
public class HTest1 : BaseHealthCheckPlus
{
    public hcteste1(IServiceProvider serviceProvider) : base(serviceProvider)
    {
    }

    public override async Task<HealthCheckResult> DoHealthCheck(HealthCheckContext context, CancellationToken cancellationToken)
    {
        return await Task.FromResult(HealthCheckResult.Healthy($"teste1"));
    }
}

//Create HealthCheck class inheriting from BaseHealthCheckPlus(IHealthCheck)
public class HTest2 : BaseHealthCheckPlus
{
    public hcteste2(IServiceProvider serviceProvider) : base(serviceProvider)
    {
    }

    public override async Task<HealthCheckResult> DoHealthCheck(HealthCheckContext context, CancellationToken cancellationToken)
    {
        return await Task.FromResult(HealthCheckResult.Degraded($"teste2"));
    }
}
//Consuming Status from HealthCheckPlus
public class MyBussines
{
    public MyBussines(IStateHealthChecksPlus healthCheckApp)
    {
        if (healthCheckApp.StatusApp.Status == HealthStatus.Degraded)
        { 
            //do something
        }
        if (healthCheckApp.StatusDep(MyEnum.HcTeste2).Status == HealthStatus.Unhealthy)
        { 
            //do something. This dependency 'HcTeste2' is not available
        }
        try
        {
            //redis access
        }
        catch (ExceptionRedis rex)
        {
            healthCheckApp.SwithToUnhealthy(MyEnum.Redis);
        }
    }
}

License

Copyright 2023 @ Fernando Cerqueira

HealthCheckPlus is licensed under the MIT license. See LICENSE.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.