英文:
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 =>
{
loggerBuilder.AddConsole();
})
.ConfigureServices((hostContext, services) => {
services.AddHealthChecks();
services.AddSingleton<IHealthCheckPublisher, SimpleHealthCheckPublisher>();
}).RunConsoleAsync();
and:
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;
}
}
I see the following output:
HealthReport received: Healthy
However, if I add a trvial health check, IHealthCheckReport.PublishAsync
is never called:
services.AddHealthChecks()
.AddCheck("test", () => HealthCheckResult.Healthy("We're good"));
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:
<PackageReference Include="Microsoft.Extensions.Diagnostics.HealthChecks.Abstractions" Version="6.0.14" />
<PackageReference Include="Microsoft.Extensions.Diagnostics.HealthChecks" Version="6.0.14" />
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论