Created
August 6, 2013 05:13
-
-
Save droyad/6162191 to your computer and use it in GitHub Desktop.
Exception format
This file contains hidden or 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 static string Format(this Exception exception, bool includeStackTrace = false) | |
| { | |
| var sb = new StringBuilder(); | |
| Format(sb, exception, false); | |
| if (includeStackTrace) | |
| { | |
| sb.AppendLine(); | |
| Format(sb, exception, true); | |
| } | |
| return sb.ToString(); | |
| } | |
| private static void Format(StringBuilder sb, Exception exception, bool includeStackTrace) | |
| { | |
| sb.AppendLine(exception.Message); | |
| var validationException = exception as DbEntityValidationException; | |
| if (validationException != null) | |
| foreach (var error in validationException.EntityValidationErrors.SelectMany(e => e.ValidationErrors)) | |
| sb.Append(error.PropertyName).Append(": ").AppendLine(error.ErrorMessage); | |
| if (includeStackTrace) | |
| { | |
| sb.AppendLine(exception.StackTrace); | |
| sb.AppendLine(); | |
| } | |
| if (exception.InnerException != null) | |
| { | |
| Format(sb, exception.InnerException, includeStackTrace); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment