See .NET Health Checks detail

1.3k views Asked by At

I've enabled .NET Health Checks on my app. I give the check a name and add a message depending on the outcome of the check. The below example shows a check called Test Health Check and the message Server Is Healthy! if a healthy result is returned. When I access the api endpoint I just see Healthy. Where do I see greater detail about the check?

.AddCheck("Test Health Check", () => HealthCheckResult.Healthy("Server Is Healthy!"))
1

There are 1 answers

0
runnerpaul On BEST ANSWER

This youtube video from @Fildor was helpful.

Here's what I did:

In Program.cs I added:

applicationBuilder.UseEndpoints(endpoints => { endpoints.MapControllers(); endpoints.MapCustomHealthChecks("Health"); });

I then created a HealthCheckExtensions class:

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Mime;
using System.Text;

namespace my.namespace.Features.HealthChecks
{

    // a custom response class for the .NET Health Check
    public static class HealthCheckExtensions
    {
        public static IEndpointConventionBuilder MapCustomHealthChecks(
            this IEndpointRouteBuilder endpoints, string serviceName)
        {
            return endpoints.MapHealthChecks("/api/health", new HealthCheckOptions
            {
                ResponseWriter = async (context, report) =>
                {
                    var result = JsonConvert.SerializeObject(
                        new HealthResult
                        {
                            Name = serviceName,
                            Status = report.Status.ToString(),
                            Duration = report.TotalDuration,
                            Info = report.Entries.Select(e => new HealthInfo
                            {
                                Key = e.Key,
                                Description = e.Value.Description,
                                Duration = e.Value.Duration,
                                Status = Enum.GetName(typeof(HealthStatus),
                                                        e.Value.Status),
                                Error = e.Value.Exception?.Message
                            }).ToList()
                        }, Formatting.None,
                        new JsonSerializerSettings
                        {
                            NullValueHandling = NullValueHandling.Ignore
                        });
                    context.Response.ContentType = MediaTypeNames.Application.Json;
                    await context.Response.WriteAsync(result);
                }
            });
        }
    }
}

A HealthInfo class:

using System;
using System.Collections.Generic;
using System.Text;

namespace my.namespace.Features.HealthChecks
{
    public class HealthInfo
    {
        public string Key { get; set; }
        public string Description { get; set; }
        public TimeSpan Duration { get; set; }
        public string Status { get; set; }
        public string Error { get; set; }
    }
}

A HealthResult class:

using System;
using System.Collections.Generic;
using System.Text;

namespace my.namespace.Features.HealthChecks
{
    public class HealthResult
    {
        public string Name { get; set; }
        public string Status { get; set; }
        public TimeSpan Duration { get; set; }
        public ICollection<HealthInfo> Info { get; set; }
    }
}