健康检查用户界面显示结果的类别

时间:2019-10-07 11:33:07

标签: asp.net-core

我已经在一个简单的.NET Core 3.0 API项目中实现了Xabaril's Healthchecks。我正在检查几个URL和SQL Server作为测试,并且已在HealthCheckUI中成功显示了结果。它们都出现在我在appSettings中定义的一个“类别”下。现在,in the documentation可以看到您可以有多个这些类别,但这似乎只应在显示来自不同来源的结果时使用。

我想做的是让我的API项目检查3个URI,并将其显示在“ Web”类别下,然后检查3个SQL Server,并将其显示在“ SQL”类别下。

从这段example here看来,我们可以用这段代码实现2个不同的类别:

            app
            .UseHealthChecks("/health", new HealthCheckOptions
            {
                Predicate = _ => true
            })
            .UseHealthChecks("/healthz", new HealthCheckOptions
            {
                Predicate = _ => true,
                ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse
            })

但是,在Startup类中,当我们添加检查时,我们没有指定将报告到哪个API端点,以便我们可以对它们进行分类: services.AddHealthChecks().AddCheck...

我是否缺少某些东西,或者不是要像这样使用此UI客户端?

1 个答案:

答案 0 :(得分:0)

我使用标签在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 Configure 方法中

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
}