Skip to content

Instantly share code, notes, and snippets.

@droyad
Created August 6, 2013 05:13
Show Gist options
  • Select an option

  • Save droyad/6162191 to your computer and use it in GitHub Desktop.

Select an option

Save droyad/6162191 to your computer and use it in GitHub Desktop.
Exception format
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