34 lines
1.1 KiB
C#
34 lines
1.1 KiB
C#
using System.Text.Json;
|
|
using Microsoft.Extensions.Diagnostics.HealthChecks;
|
|
|
|
public static class HealthCheckResponseWriter
|
|
{
|
|
private static readonly JsonSerializerOptions JsonOptions = new()
|
|
{
|
|
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
|
|
WriteIndented = true
|
|
};
|
|
|
|
public static async Task WriteAsync(HttpContext context, HealthReport report)
|
|
{
|
|
context.Response.ContentType = "application/json";
|
|
|
|
var response = new
|
|
{
|
|
status = report.Status.ToString(),
|
|
totalDurationMs = report.TotalDuration.TotalMilliseconds,
|
|
checks = report.Entries.Select(e => new
|
|
{
|
|
name = e.Key,
|
|
status = e.Value.Status.ToString(),
|
|
durationMs = e.Value.Duration.TotalMilliseconds,
|
|
description = e.Value.Description,
|
|
data = e.Value.Data.Count > 0 ? e.Value.Data : null,
|
|
exception = e.Value.Exception?.Message
|
|
})
|
|
};
|
|
|
|
await context.Response.WriteAsync(JsonSerializer.Serialize(response, JsonOptions));
|
|
}
|
|
}
|