From postings on the MSDN Forums and my own work, I have a template that allows for a quick read of an exception in C#. The following code will format the exception so it can be read on either the console or in a message box.

public static string Create(Exception ex)
{
 return string.Format(
   "Exception:\t{1}{0}Message:\t{2}{0}Source:\t\t{3}{0}TargetSite:\t{4}{0}{0}StackTrace{0}--------------{0}{0}{5}{0}",
   System.Environment.NewLine,
   ex.GetType(),
   ex.Message.Replace("\r", "").Replace('\n', '\t'),
   ex.Source,
   ex.TargetSite.ToString(),
   ex.StackTrace);
}

Here is what it look likes if you pipe it to a message box as in MessageBox.Show( MyClass.Create ( ex ) )

Message Box View of the Method's output.

As one can see all the important information is nicely spaced out and readable.

Share

Tags: ,