请参阅.NET运行状况检查的详细信息



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

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

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

以下是我所做的:

在程序.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; }
}
}

相关内容

最新更新