【问题标题】:See .NET Health Checks detail请参阅 .NET 健康检查详细信息
【发布时间】:2020-09-30 14:03:41
【问题描述】:

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

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

【问题讨论】:

标签: c# .net health-check


【解决方案1】:

来自@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-07-27
    • 1970-01-01
    • 1970-01-01
    • 2019-06-05
    • 1970-01-01
    • 2011-03-20
    • 2013-09-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多