After several embarrassing attempts at debugging issues on our deployed C# application I've discovered I can get the most detail out of an exception simply by getting someException.toString().
Since I'm logging this value to the database I'm just a little bit nervous about how large an exception string may get. My current errorDetail field is set to a maximum of 3000 characters.
Is this a good limit or is there a possibility of getting exceptions that are larger?
The answer is: no, there's not really a safe limit - and you'll have to manually trim down the length if it's too long.
There's two easy ways the Exception.ToString() could get really long.
Reason A - the Exception.Message is long.
The person throwing the exception can put a really long message in the exception. Which, really, is awesome - it means they're trying to capture as much detail into what caused the exception, to help hunt down the problem.
Reason B - The Stack Trace Could Be Long.
If the exception occurs 20 levels deep function-wise, the stack trace will have 20 levels of detail.
Here's some simple code to demonstrate the concept:
private string GetExceptionString(int iter)
{
try
{
return RecursiveFunction(iter);
}
catch (Exception ex)
{
return ex.ToString();
}
}
private string RecursiveFunction(int iter)
{
if (iter > 1)
return RecursiveFunction(iter - 1);
throw new Exception("Actual Exception Occurs Here");
}
Basically, if you call GetExceptionString(1000), it'll cause an exception within 1000 levels of nested functions. And, sure enough, you'll get back an Exception with 1000 levels of detail in the Stack Trace, and be around a hundred-thousand characters long.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With