Xabaril / AspNetCore.Diagnostics.HealthChecks

Enterprise HealthChecks for ASP.NET Core Diagnostics Package
Apache License 2.0
3.98k stars 771 forks source link

Log the Exception If One Occurs During Health Check #2196

Open jperson2000 opened 2 months ago

jperson2000 commented 2 months ago

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!