Recently i’ve encountered a problem of getting as much details from exception as i possibly could. The reason? Well, when you need to solve problems in shipped product the log is usually the only thing you have.
Obviously
Exception.ToString()
works pretty well but it is not very helpful when you deal with FaultException and who knows what surprises can custom exceptions give you.
So what is the best way to get exception details with decent level of paranoia?
I’ve looked around and googled on the topic. Susrpisingly there are not so many discussions on this. Anyway I tried to compound quintessence here.
Talk is cheap. Show me the code.
Here we have the sample code throwing the exception.
Here is the method GetExtendedExceptionDetails:
As you can see we use Reflection to get instance properties and then get their values. I know it is expensive but we don’t really know what possible properties the concrete exception exposes. And we all hope that errors won’t occur so often in application that it would kill the performance.
Now let’s look at what we actually gained.
This is what Exception.ToString returns:
And this returns our new method:
We use log4net for logging and to reduce the performance overhead there is ILog.IsErrorEnabled property. I just check it before calling the extended exception handling.