tags:

views:

111

answers:

1

Anyone have a good way to output an Exception in C# to a string that handles inner exceptions and the such? I am sure every developer has her own implimentation of this, but I can't find my old one and want to see what better ones are out there.

Here is one without recursion for inner exceptions as an example:

var Message = String.Format("Exception Message: \n{0}\n\nStackTrace: \n{1}", ex.Message, ex.StackTrace);
+10  A: 

Simply method ToString is the best:

ex.ToString();
mykhaylo
+1, it's not obvious to a novice as most other framework classes just return the type name in a ToString(), but `Exception.ToString()` has all the information you need.
Matt Howells
+1, and in addition to Matt's comment one should note that *most* (but unfortunately not all) Exceptions write information about auxiliary properties they have into the ToString() output. If you just use "Message" and "StackTrace" you generally miss out on such properties.
Christian.K