Skip to content

Instantly share code, notes, and snippets.

@Nilzor
Created February 8, 2013 08:43
Show Gist options
  • Select an option

  • Save Nilzor/4737502 to your computer and use it in GitHub Desktop.

Select an option

Save Nilzor/4737502 to your computer and use it in GitHub Desktop.
A handy Exception extension class exposing GetAsString()
public static class ExceptionExtensions
{
public static string GetAsString(this Exception ex)
{
StringBuilder sb = new StringBuilder();
AppendDefaultExceptionString(ex, sb);
return sb.ToString();
}
private static void AppendDefaultExceptionString(this Exception ex, StringBuilder sb)
{
sb.AppendLine(ex.GetType().ToString());
sb.AppendLine(ex.Message);
sb.AppendLine(ex.StackTrace);
sb.AppendLine();
if (ex.InnerException != null)
{
sb.AppendLine("---Inner exception: ");
AppendDefaultExceptionString(ex.InnerException, sb);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment