Skip to content

Instantly share code, notes, and snippets.

@khmylov
Created April 24, 2012 10:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save khmylov/2478592 to your computer and use it in GitHub Desktop.
Save khmylov/2478592 to your computer and use it in GitHub Desktop.
Helper method to flatten AggregateException hierarchy into a plain text.
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);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment