Salix.AspNetCore.HealthCheck 1.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package Salix.AspNetCore.HealthCheck --version 1.0.0
NuGet\Install-Package Salix.AspNetCore.HealthCheck -Version 1.0.0
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="Salix.AspNetCore.HealthCheck" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Salix.AspNetCore.HealthCheck --version 1.0.0
#r "nuget: Salix.AspNetCore.HealthCheck, 1.0.0"
#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 Salix.AspNetCore.HealthCheck as a Cake Addin
#addin nuget:?package=Salix.AspNetCore.HealthCheck&version=1.0.0

// Install Salix.AspNetCore.HealthCheck as a Cake Tool
#tool nuget:?package=Salix.AspNetCore.HealthCheck&version=1.0.0

Salix.AspNetCore.HealthCheck

Package provides two functionalities for AspNet (Core) APIs:

  • Custom formatter of health check results as JSON object (default response is plain-text)
  • Page, displaying health check results in humanly readable format in colors.

JSON fomatter

Returns all defined health checks as JSOn object with implemented details and error handling (exception details).
Example response:

{
    "status": "Healthy",
    "checks": [
        {
            "key": "Database",
            "status": "Healthy",
            "description": "Database is OK.",
            "exception": null,
            "data": [
                {
                    "key": "ConnString",
                    "value": "Connection string (shown only in developer mode)"
                }
            ]
        },
        {
            "key": "ExtApi",
            "status": "Healthy",
            "description": "ExtAPI is OK.",
            "exception": null,
            "data": [
                {
                    "key": "ExtApi URL",
                    "value": "https://extapi.com/api"
                },
                {
                    "key": "User",
                    "value": "username from config"
                },
                {
                    "key": "Password",
                    "value": "password from config"
                },
                {
                    "key": "Token",
                    "value": "Secret token from config"
                }
            ]
        }
    ]
}

Health check page

Controller action content compiler to issue HTML page with health check results: Health check page

Additional possibility to add some custom links to implemented functionalities for some sandbox or testing links (or anything else) to thi page.
NOTE: Does NOT bring entire MVC stack to display page.

Usage

Register your health checks normally as described in official Microsoft documentation:

builder.Services.AddHealthChecks()
    .Add(new HealthCheckRegistration("Database", sp => new DummyDatabaseHealthCheck(builder.Environment.IsDevelopment()), HealthStatus.Unhealthy, null, TimeSpan.FromSeconds(10)))
    .Add(new HealthCheckRegistration("ExtApi", sp => new DummyExternalApiHealthCheck(builder.Environment.IsDevelopment()), HealthStatus.Unhealthy, null, TimeSpan.FromSeconds(5)));

Then for WebApplication builder, use provided extension to use JSON fomatter:

app.UseJsonHealthChecks("/health", builder.Environment.IsDevelopment());

Health page

To add health page (humanized view of health check results), create or use existing controller and add action endpoint like in sample below:

[ApiController]
public class HealthCheckController : ControllerBase
{
    private readonly HealthCheckService _healthChecks;

    public HealthCheckController(HealthCheckService healthChecks) =>
        _healthChecks = healthChecks;

    [HttpGet("/healthpage")]
    public async Task<ContentResult> ShowHealth()
    {
        var healthResult = await _healthChecks.CheckHealthAsync();
        return new ContentResult
        {
            ContentType = "text/html",
            StatusCode = (int)HttpStatusCode.OK,
            Content = HealthTestPage.GetContents(
                healthReport: healthResult,
                originalHealthTestEndpoint: "/health",
                testingLinks: new List<HealthTestPageLink>
                {
                        new HealthTestPageLink { TestEndpoint = "/api/sample/exception", Name = "Exception", Description = "Throws dummy exception/error to check Json Error functionality." },
                        new HealthTestPageLink { TestEndpoint = "/api/sample/validation", Name = "Validation Error", Description = "Throws dummy data validation exception to check Json Error functionality for data validation." },
                }),
        };
    }
}
Product Compatible and additional computed target framework versions.
.NET 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 was computed.  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. 
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.

Version Downloads Last updated
1.1.0 521 11/20/2023
1.0.0 248 1/6/2023

Initial: Moved from aspNetCore.Utilities meta-package.