dotnet-cqrs/docs/observability/health-checks/aspnetcore-integration.md

1.1 KiB

ASP.NET Core Integration

Integrate event streaming health checks with ASP.NET Core.

Quick Start

builder.Services.AddHealthChecks()
    .AddCheck<StreamHealthCheck>("event-streams")
    .AddCheck<ConsumerHealthCheck>("consumers")
    .AddCheck<ProjectionHealthCheck>("projections");

app.MapHealthChecks("/health");
app.MapHealthChecks("/health/ready", new HealthCheckOptions
{
    Predicate = check => check.Tags.Contains("ready")
});

Custom Response Writer

app.MapHealthChecks("/health/detail", new HealthCheckOptions
{
    ResponseWriter = async (context, report) =>
    {
        context.Response.ContentType = "application/json";
        await context.Response.WriteAsJsonAsync(new
        {
            status = report.Status.ToString(),
            duration = report.TotalDuration.TotalMilliseconds,
            checks = report.Entries.Select(e => new
            {
                name = e.Key,
                status = e.Value.Status.ToString(),
                description = e.Value.Description,
                data = e.Value.Data
            })
        });
    }
});

See Also