C# Aspnetcore healthchecks已忽略降级状态

C# Aspnetcore healthchecks已忽略降级状态,c#,asp.net-core,health-check,C#,Asp.net Core,Health Check,我是根据MSDN网站上发布的信息了解健康检查的: 下面的代码片段告诉我们,如果一个不健康的检查返回,我可以通过说它已降级来覆盖该值 services.AddHealthChecks() .AddCheck<ExampleHealthCheck>( "example_health_check", failureStatus: HealthStatus.Degraded, tags: new[] { "

我是根据MSDN网站上发布的信息了解健康检查的:

下面的代码片段告诉我们,如果一个不健康的检查返回,我可以通过说它已降级来覆盖该值

services.AddHealthChecks()
    .AddCheck<ExampleHealthCheck>(
        "example_health_check",
        failureStatus: HealthStatus.Degraded,
        tags: new[] { "example" });
services.AddHealthChecks()
.AddCheck(
“健康检查示例”,
故障状态:HealthStatus.Degraded,
标签:新[]{“示例”});
因此,我尝试了这个实现,假设我会得到一个降级的结果,而不是一个不健康的结果:

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }
    public IConfiguration Configuration { get; }
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddHealthChecks().AddCheck<ExampleHealthCheck>("ExampleHealthCheck", failureStatus: HealthStatus.Degraded);
    }
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        app.UseRouting();
        app.UseEndpoints(endpoints =>
        {
            endpoints.MapHealthChecks("/health");
        });
    }
}
internal class ExampleHealthCheck : IHealthCheck
{
    public Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default(CancellationToken))
    {
        return Task.FromResult(HealthCheckResult.Unhealthy("An unhealthy result."));
    }
}
公共类启动
{
公共启动(IConfiguration配置)
{
配置=配置;
}
公共IConfiguration配置{get;}
public void配置服务(IServiceCollection服务)
{
services.AddHealthChecks().AddCheck(“ExampleHealthCheck”,failureStatus:HealthStatus.Degraded);
}
public void配置(IApplicationBuilder应用程序、IWebHostEnvironment环境)
{
app.UseRouting();
app.UseEndpoints(端点=>
{
端点。MapHealthChecks(“/health”);
});
}
}
内部类示例HealthCheck:IHealthCheck
{
公共任务CheckHealthAsync(HealthCheckContext上下文,CancellationToken CancellationToken=default(CancellationToken))
{
返回Task.FromResult(HealthCheckResult.unhealth(“一个不健康的结果”);
}
}

有人能解释一下为什么这不起作用或者我哪里弄错了吗?

这很让人困惑,因为您希望HealthCheck会自动返回您在
failureStatus
属性上定义的结果,但HealthCheck会显式返回
Unhealth

如果查看上的
AddHealthCheck
方法,您将看到中仅创建了
HealthCheckRegistration
的实例

public static IHealthChecksBuilder AddCheck(
            this IHealthChecksBuilder builder,
            string name,
            IHealthCheck instance,
            HealthStatus? failureStatus = null,
            IEnumerable<string>? tags = null,
            TimeSpan? timeout = null)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            if (name == null)
            {
                throw new ArgumentNullException(nameof(name));
            }

            if (instance == null)
            {
                throw new ArgumentNullException(nameof(instance));
            }

            return builder.Add(new HealthCheckRegistration(name, instance, failureStatus, tags, timeout));
        }

您是否尝试过更改
示例healthcheck
checkhealthsync
的返回值?因此,与其返回
不健康
,不如让代码返回例如
健康
public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }
    public IConfiguration Configuration { get; }
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddHealthChecks().AddCheck<ExampleHealthCheck>("ExampleHealthCheck", failureStatus: HealthStatus.Degraded);
    }
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        app.UseRouting();
        app.UseEndpoints(endpoints =>
        {
            endpoints.MapHealthChecks("/health");
        });
    }
}

internal class ExampleHealthCheck : IHealthCheck
{
   public Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default(CancellationToken))
   {
      return Task.FromResult(new HealthCheckResult(context.Registration.FailureStatus));
   }
}