【问题标题】:HealthCheckResult how to return additional info in response?HealthCheckResult 如何返回附加信息作为响应?
【发布时间】:2019-10-02 00:05:28
【问题描述】:
Microsoft.Extensions.Diagnostics.HealthChecks.HealthCheckResult

public HealthCheckResult (Microsoft.Extensions.Diagnostics.HealthChecks.HealthStatus status, string description = null, Exception exception = null, System.Collections.Generic.IReadOnlyDictionary<string,object> data = null);

Parameters

status HealthStatus  
A value indicating the status of the component that was checked.

description String  
A human-readable description of the status of the component that was checked.

exception Exception  
An Exception representing the exception that was thrown when checking for status (if any).

data IReadOnlyDictionary<String,Object> 
Additional key-value pairs describing the health of the component.

不幸的是,试错表明description 未在运行状况检查端点响应中返回。

catch(Exception ex)
{
    return new HealthCheckResult(status: HealthStatus.Unhealthy, 
                                 description: ex.Message,
                                 exception: ex);
}

我认为异常消息会在响应中返回并显示在浏览器中,但事实并非如此。

除了DegradedHealthyUnhealthy等之外,是否有一种未记录的机制来返回其他信息?

【问题讨论】:

    标签: asp.net-core-webapi health-monitoring


    【解决方案1】:

    HealthCheckResult 是 .Net Core 中的一个结构。在IHealthCheck接口的CheckHealthAsync方法的实现中,您可以返回状态以及自定义描述如下 -

       if (response.StatusCode == HttpStatusCode.OK)
       {
              return HealthCheckResult.Healthy("connected successfully.");
       }
    

    return HealthCheckResult.Unhealthy("Failed to connect.)
    

    【讨论】:

    • 你知道为什么带有扩展构造函数的HealthCheckResult(我已经编辑过我的帖子)不返回description文本吗?
    • @BaltoStar 我不知道为什么HealthCheckResult 扩展构造函数没有返回自定义文本。但根据 Microsoft 标准文档,建议实现 IHealthCheck 并使用 HealthCheckResult 结构返回状态,如我在上面的示例中所示(这将返回自定义文本)。希望这会有所帮助!
    • 顺便说一句,在 asp .net core 3.1 中有第二个参数接受字符串和对象的字典,您可以在其中添加更多信息。
    猜你喜欢
    • 1970-01-01
    • 2023-02-03
    • 2020-01-04
    • 1970-01-01
    • 2011-08-30
    • 2017-09-27
    • 2018-01-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多