Skip to content

Instantly share code, notes, and snippets.

@khmylov
Created April 24, 2012 10:10
Show Gist options
  • Select an option

  • Save khmylov/2478592 to your computer and use it in GitHub Desktop.

Select an option

Save khmylov/2478592 to your computer and use it in GitHub Desktop.

Revisions

  1. khmylov created this gist Apr 24, 2012.
    29 changes: 29 additions & 0 deletions gistfile1.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,29 @@
    public string GetExceptionLog(Exception exception)
    {
    return UnwrapExceptionTextLoop(new StringBuilder(), 0, exception);
    }

    private string UnwrapExceptionTextLoop(StringBuilder sb, int level, Exception e)
    {
    if (e == null)
    {
    return sb.ToString();
    }

    for (int i = 0; i < level; i++)
    {
    sb.Append(">>");
    }

    var aggregate = e as AggregateException;

    sb.AppendFormat("{0} - {1} [{2}] {3}", e.GetType(), aggregate == null ? e.Message : String.Empty, e.StackTrace, Environment.NewLine);

    if (aggregate != null)
    {
    return String.Join(Environment.NewLine,
    aggregate.InnerExceptions.Select(inner => UnwrapExceptionTextLoop(sb, level + 1, inner)));
    }

    return UnwrapExceptionTextLoop(sb, level + 1, e.InnerException);
    }