asp.net-core - 健康检查 UI 按类别显示结果
问题描述
我在一个简单的API 项目中实现了Xabaril 的 Healthchecks 。.NET Core 3.0
我正在检查几个 URL 和 SQL 服务器作为测试,我成功地在 HealthCheckUI 中显示了结果。它们都出现在我在 中定义的一个“类别”下appSettings
。现在,在文档中,您可以看到您可以拥有多个这些类别,但似乎这仅在您显示来自不同来源的结果时使用。
我想做的是让我的 API 项目检查 3 个 URI 并将它们显示在“Web”类别下,然后检查 3 个 SQL 服务器并将它们显示在“SQL”类别下。
从这里的这个例子看来,我们可以用这段代码实现 2 个不同的类别:
app
.UseHealthChecks("/health", new HealthCheckOptions
{
Predicate = _ => true
})
.UseHealthChecks("/healthz", new HealthCheckOptions
{
Predicate = _ => true,
ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse
})
但是在 Startup 类中,当我们添加检查时,我们没有指定它们应该报告给哪个 API 端点,以便我们可以对它们进行分类:
services.AddHealthChecks().AddCheck...
我是否遗漏了什么,或者这个 UI 客户端不适合这样使用?
解决方案
我在使用标签的 Net Core 3.1 项目中得到了这个工作。
例如,如果您想要 Spa UI 中的数据库和 API 部分:
在Startup.cs的ConfigureServices方法中
services.AddHealthChecks()
.AddSqlServer("ConnectionString", name: "Application Db", tags: new[] { "database" })
.AddHangfire(options => { options.MinimumAvailableServers = 1; }, "Hangfire Db", tags: new[] { "database" })
.AddUrlGroup(new Uri("https://myapi.com/api/person"), HttpMethod.Get, "Person Endpoint", tags: new[] { "api" })
.AddUrlGroup(new Uri("https://myapi.com/api/organisation"), HttpMethod.Get, "Org Endpoint", tags: new[] { "api" })
.AddUrlGroup(new Uri("https://myapi.com/api/address"), HttpMethod.Get, "Address Endpoint", tags: new[] { "api" });
在Startup.cs的配置方法中
config.MapHealthChecks("/health-check-database", new HealthCheckOptions
{
Predicate = r => r.Tags.Contains("database"),
ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse
});
config.MapHealthChecks("/health-check-api", new HealthCheckOptions
{
Predicate = r => r.Tags.Contains("api"),
ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse
});
然后appsettings.json类似:
"HealthChecksUI": {
"HealthChecks": [
{
"Name": "Database Health Checks",
"Uri": "https://myapi.com/api/health-check-database"
},
{
"Name": "API Health Checks",
"Uri": "https://myapi.com/api/health-check-api"
}
],
"Webhooks": [],
"EvaluationTimeinSeconds": 10,
"MinimumSecondsBetweenFailureNotifications": 60
}