Get the entire information of an Exception by recursive looping on the inner Exception property

By [)ia6l0 iii

A simple function to recursively loop thru the inner exception and retrieve the entire information about an exception.

public static string GetFormattedException(Exception ex, bool isRecursive)
{
StringBuilder exceptionString = new StringBuilder();

if (isRecursive)
exceptionString.AppendFormat(""************ Inner Exception ************{0}"", Environment.NewLine);
else
exceptionString.AppendFormat(""*************** Exception ***************{0}"", Environment.NewLine);

exceptionString.AppendFormat(""Exception Message:{0}{1}{2}"", Environment.NewLine, ex.Message, Environment.NewLine);
exceptionString.AppendFormat(""Stack Trace:{0}{1}{2}"", Environment.NewLine, ex.StackTrace, Environment.NewLine);
exceptionString.AppendFormat(""Source:{0}{1}{2}"", Environment.NewLine, ex.Source, Environment.NewLine);

//recurse into inner exceptions
if (ex.InnerException != null)
{
exceptionString.Append(String.Format(""{0}{1}"", GetFormattedException(ex.InnerException, true), Environment.NewLine));
}
return exceptionString.ToString();
}

Get the entire information of an Exception by recursive looping on the inner Exception property  (1400 Views)