AspNetCore.Diagnostics.HealthChecks icon indicating copy to clipboard operation
AspNetCore.Diagnostics.HealthChecks copied to clipboard

Log the Exception If One Occurs During Health Check

Open jperson2000 opened this issue 10 months ago • 0 comments

What would you like to be added:

Calls to Log.LogError(Exception) if an exception occurs during a health check.

Why is this needed:

Currently, if an exception occurs during a health check, the exception will be included in the HealthCheckResult but nothing further happens to notify of the exception, causing it to be ignored. This makes troubleshooting difficult when there's only the word Unhealthy to go on.

https://github.com/Xabaril/AspNetCore.Diagnostics.HealthChecks/blob/2f6a10f89e3c6d97f232f4157a80e3a9e1470dc5/src/HealthChecks.AzureServiceBus/AzureServiceBusQueueHealthCheck.cs#L25-L37

Proposed change:

Add a call to log.LogError(ex) to log the exception via ILogger<T>, like this:

try 
 { 
     if (Options.UsePeekMode) 
         await CheckWithReceiver().ConfigureAwait(false); 
     else 
         await CheckWithManagement().ConfigureAwait(false); 
  
     return HealthCheckResult.Healthy(); 
 } 
 catch (Exception ex) 
 { 
     log.LogError(ex, "Health Check for Azure Service Bus failed.");   <---------------
     return new HealthCheckResult(context.Registration.FailureStatus, exception: ex); 
 } 

I'm happy to PR this if you think it's a good change. Thanks!

jperson2000 avatar Apr 12 '24 19:04 jperson2000