NickCraver / StackExchange.Exceptional

Error handler used for the Stack Exchange network
https://nickcraver.com/StackExchange.Exceptional/
Apache License 2.0
863 stars 171 forks source link

Any way to log the InnerException from exceptions? #216

Closed IronSean closed 2 years ago

IronSean commented 2 years ago

Without the InnerException the exception logs are at times almost useless, I thought it would be capturing inner exceptions recursively by default.

NickCraver commented 2 years ago

Can you explain what you mean? The stack trace for both is in the Details, and .Data is captured for both (recursively). You can, on log, directly trigger a log for the inner exception as well but that's going to result in a lot of duplication for the average use case. For example if you use EntityFramework this would generally result in 4 errors logged per exception and get quite noisy. But if you want to modify what happens when an error is logged you can hook into the .OnBeforeLog of .OnAfterLog events and make it as noisy as you want :)

IronSean commented 2 years ago

Thank you for the response. This one is user error:

On logs I was reviewing the inner exceptions were not being captured recursively as expected. Then when researching I found #52 where a custom .OnAfterLog handler was suggested to capture the inner exceptions which didn't seem correct.

But taking another look, you are correct that the inner exceptions are being recursively captured. Around the same time I also found a couple instances where some code looked like this

catch (Exception e)
{
  throw new CustomException("custom error here");
}

instead of this

catch (Exception e)
{
  throw new CustomException("custom error here", ex);
}

meaning some of the logs I was examining had failed to preserve the inner exception entirely.