Adding health checks with AddHealthChecks().AddCheck() prevents calls to IHeathCheckPublisher.PublishChecks

huangapple go评论65阅读模式
英文:

Adding health checks with AddHealthChecks().AddCheck() prevents calls to IHeathCheckPublisher.PublishChecks

问题

在给定的代码中,我看到了对IHealthCheckPublisher.PublishAsync的调用:

await Host.CreateDefaultBuilder()
   .ConfigureLogging(loggerBuilder =>
   {
       loggerBuilder.AddConsole();
   })
   .ConfigureServices((hostContext, services) => {
       services.AddHealthChecks();
       services.AddSingleton<IHealthCheckPublisher, SimpleHealthCheckPublisher>();
   }).RunConsoleAsync();

以及:

internal class SimpleHealthCheckPublisher : IHealthCheckPublisher
{
    private readonly ILogger<SimpleHealthCheckPublisher> logger;

    public SimpleHealthCheckPublisher(ILogger<SimpleHealthCheckPublisher> logger)
    {
        this.logger = logger;
    }

    public Task PublishAsync(HealthReport report, CancellationToken cancellationToken)
    {
        logger.LogInformation("HealthReport received: {Status}", report.Status);

        return Task.CompletedTask;
    }
}

我看到了以下输出:

HealthReport received: Healthy

然而,如果我添加一个简单的健康检查,IHealthCheckReport.PublishAsync从未被调用:

services.AddHealthChecks()
    .AddCheck("test", () => HealthCheckResult.Healthy("We're good"));

我如何添加一个健康检查但仍然调用IHealthCheckReport.PublishAsync

更新 #1:
通过将 TargetFramework 设置为 net7.0,这段代码可以正常工作。但不清楚为什么。不幸的是,我需要在 net6.0 上运行。

英文:

Given the following code, I see calls to IHealthCheckPublisher.PublishAsync:

await Host.CreateDefaultBuilder()
   .ConfigureLogging(loggerBuilder =&gt;
   {
       loggerBuilder.AddConsole();
   })
   .ConfigureServices((hostContext, services) =&gt; {
       services.AddHealthChecks();
       services.AddSingleton&lt;IHealthCheckPublisher, SimpleHealthCheckPublisher&gt;();
   }).RunConsoleAsync();

and:

internal class SimpleHealthCheckPublisher : IHealthCheckPublisher
{
    private readonly ILogger&lt;SimpleHealthCheckPublisher&gt; logger;

    public SimpleHealthCheckPublisher(ILogger&lt;SimpleHealthCheckPublisher&gt; logger)
    {
        this.logger = logger;
    }

    public Task PublishAsync(HealthReport report, CancellationToken cancellationToken)
    {
        logger.LogInformation(&quot;HealthReport received: {Status}&quot;, report.Status);

        return Task.CompletedTask;
    }
}

I see the following output:

HealthReport received: Healthy

However, if I add a trvial health check, IHealthCheckReport.PublishAsync is never called:

services.AddHealthChecks()
    .AddCheck(&quot;test&quot;, () =&gt; HealthCheckResult.Healthy(&quot;We&#39;re good&quot;));

How do I add a health check but still get IHealthCheckReport.PublishAsync to be called?

Full repro available here:https://github.com/anvilcloud/HealthChecksRepro

Update #1:
This works by setting the TargetFramework to net7.0. Unsure why. Unfortunately, I need it to work for net6.0.

答案1

得分: 0

降级 HealthChecks 包对我有用:

<PackageReference Include="Microsoft.Extensions.Diagnostics.HealthChecks.Abstractions" Version="6.0.14" />
<PackageReference Include="Microsoft.Extensions.Diagnostics.HealthChecks" Version="6.0.14" />
英文:

Downgrading the HealthChecks packages worked for me:

&lt;PackageReference Include=&quot;Microsoft.Extensions.Diagnostics.HealthChecks.Abstractions&quot; Version=&quot;6.0.14&quot; /&gt;
&lt;PackageReference Include=&quot;Microsoft.Extensions.Diagnostics.HealthChecks&quot; Version=&quot;6.0.14&quot; /&gt;

huangapple
  • 本文由 发表于 2023年2月16日 02:18:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/75463929.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定