Created
April 24, 2012 10:10
-
-
Save khmylov/2478592 to your computer and use it in GitHub Desktop.
Helper method to flatten AggregateException hierarchy into a plain text.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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