Skip to content

Instantly share code, notes, and snippets.

@ddikman
Created September 6, 2016 13:04
Show Gist options
  • Save ddikman/eb03f48679044226f3d8cc693029b87b to your computer and use it in GitHub Desktop.
Save ddikman/eb03f48679044226f3d8cc693029b87b to your computer and use it in GitHub Desktop.
C# extension to dump the entire stack trace and hierarchy of an exception
public static class ExceptionFormatterExtension
{
public static string GetDump(this Exception e)
{
var exceptions = new Stack<Exception>();
while (e != null)
{
exceptions.Push(e);
e = e.InnerException;
}
StringBuilder sb = new StringBuilder();
while (exceptions.Count > 0)
{
e = exceptions.Pop();
sb.AppendLine(e.GetType().Name + ": " + e.Message);
sb.AppendLine(e.StackTrace);
sb.AppendLine();
}
return sb.ToString().Trim();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment