BaltoStar
BaltoStar

Reputation: 8977

HealthCheckResult how to return additional info in response?

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.

Unfortunately trial-and-error shows that description is not returned in the health-check endpoint response.

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

I'd think that the exception message would be returned in the response and displayed in the browser but it's not.

Is there an undocumented mechanism to return additional info beyond Degraded Healthy Unhealthy etc. ?

Upvotes: 2

Views: 2614

Answers (1)

Aparna Gadgil
Aparna Gadgil

Reputation: 468

The HealthCheckResult is a struct in .Net Core. In the implementation of method CheckHealthAsync of IHealthCheck interface, you can return the status along with custom description as below -

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

OR

return HealthCheckResult.Unhealthy("Failed to connect.)

Upvotes: 1

Related Questions