Skip to content

Instantly share code, notes, and snippets.

@smling
Created February 27, 2022 09:37
Show Gist options
  • Save smling/f00866e2d8337996f91ebb65d8edc220 to your computer and use it in GitHub Desktop.
Save smling/f00866e2d8337996f91ebb65d8edc220 to your computer and use it in GitHub Desktop.
Configure heath check endpoint with custom option.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseHealthChecks("/health", new HealthCheckOptions
{
ResponseWriter = async (context, report) =>
{
context.Response.ContentType = "application/json";
HealthCheckResponse response = new HealthCheckResponse
{
Status = report.Status.ToString(),
HealthCheckProbes = report.Entries.Select(o => new HealthCheckProbe
{
Component = o.Key,
Description = o.Value.Description,
Status = o.Value.Status.ToString(),
Duration = o.Value.Duration,
Exception = o.Value.Exception == null ? string.Empty : o.Value.Exception.ToString(),
Message = o.Value.Exception == null ? string.Empty : o.Value.Exception.Message,
}),
Duration = report.TotalDuration,
};
await context.Response.Body.WriteAsync(JsonSerializer.SerializeToUtf8Bytes(response));
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment