Last active
July 18, 2024 13:22
-
-
Save cemerson/81d6db6cde0c1f10480e0258f9e28efd to your computer and use it in GitHub Desktop.
Get C# Exception Details String (HTML)
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
/* ---------- Usage ---------------------------------- | |
string exceptionDetailsHtml = ExceptionHelper.GetExceptionDetailsInNicelyFormatted(ex, "HTML"); | |
Console.WriteLine(exceptionDetailsHtml); | |
string exceptionDetailsText = ExceptionHelper.GetExceptionDetailsInNicelyFormatted(ex, "TEXT"); | |
Console.WriteLine(exceptionDetailsText); | |
// ---------------------------------------------------- */ | |
public static class ExceptionHelper | |
{ | |
public static string GetExceptionDetailsInNicelyFormatted(Exception ex, string displayFormat = "TEXT") | |
{ | |
if (ex == null) return string.Empty; | |
if (displayFormat.ToUpper() == "HTML") | |
{ | |
return GetExceptionDetailsInHTML(ex); | |
} | |
else | |
{ | |
return GetExceptionDetailsInPlainText(ex); | |
} | |
} | |
private static string GetExceptionDetailsInHTML(Exception ex) | |
{ | |
StringBuilder sb = new StringBuilder(); | |
sb.Append("<html><head><style>"); | |
sb.Append("body { font-family: Arial, sans-serif; }"); | |
sb.Append("h1 { color: #b22222; }"); | |
sb.Append(".exception { margin: 10px 0; padding: 10px; border: 1px solid #ccc; background-color: #f9f9f9; }"); | |
sb.Append(".details { font-size: 0.9em; color: #333; }"); | |
sb.Append("</style></head><body>"); | |
sb.Append("<h1>Exception Details</h1>"); | |
FormatExceptionDetailsHTML(sb, ex); | |
sb.Append("</body></html>"); | |
return sb.ToString(); | |
} | |
private static void FormatExceptionDetailsHTML(StringBuilder sb, Exception ex) | |
{ | |
if (ex == null) return; | |
sb.Append("<div class='exception'>"); | |
sb.AppendFormat("<h2>{0}</h2>", ex.GetType().Name); | |
sb.AppendFormat("<div class='details'><strong>Message:</strong> {0}</div>", ex.Message); | |
sb.AppendFormat("<div class='details'><strong>Source:</strong> {0}</div>", ex.Source); | |
sb.AppendFormat("<div class='details'><strong>Stack Trace:</strong><pre>{0}</pre></div>", ex.StackTrace); | |
if (ex.InnerException != null) | |
{ | |
sb.Append("<div class='details'><strong>Inner Exception:</strong>"); | |
FormatExceptionDetailsHTML(sb, ex.InnerException); | |
sb.Append("</div>"); | |
} | |
sb.Append("</div>"); | |
} | |
private static string GetExceptionDetailsInPlainText(Exception ex) | |
{ | |
StringBuilder sb = new StringBuilder(); | |
sb.AppendLine("Exception Details:"); | |
FormatExceptionDetailsPlainText(sb, ex, 0); | |
return sb.ToString(); | |
} | |
private static void FormatExceptionDetailsPlainText(StringBuilder sb, Exception ex, int indentLevel) | |
{ | |
if (ex == null) return; | |
string indent = new string('\t', indentLevel); | |
sb.AppendLine($"{indent}Exception Type: {ex.GetType().Name}"); | |
sb.AppendLine($"{indent}Message: {ex.Message}"); | |
sb.AppendLine($"{indent}Source: {ex.Source}"); | |
sb.AppendLine($"{indent}Stack Trace:"); | |
sb.AppendLine($"{indent}{ex.StackTrace}"); | |
if (ex.InnerException != null) | |
{ | |
sb.AppendLine($"{indent}Inner Exception:"); | |
FormatExceptionDetailsPlainText(sb, ex.InnerException, indentLevel + 1); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment