1

我已.NET Health Checks在我的应用程序上启用。我给支票起一个名字,并根据支票的结果添加一条消息。下面的示例显示了调用的检查以及返回健康结果时Test Health Check的消息。Server Is Healthy!当我访问 api 端点时,我只看到Healthy. 我在哪里可以看到有关支票的更多详细信息?

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

1 回答 1

0

@Fildor 的这个youtube视频很有帮助。

这是我所做的:

在 Program.cs 我添加了:

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

然后我创建了一个HealthCheckExtensions类:

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);
                }
            });
        }
    }
}

一个HealthInfo类:

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; }
    }
}

一个HealthResult类:

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; }
    }
}
于 2020-09-30T06:03:41.727 回答