Created
January 8, 2013 23:29
-
-
Save dkarzon/4489059 to your computer and use it in GitHub Desktop.
Function to format a stack trace to 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
public static string FormatStackTrace(string stacktrace) | |
{ | |
try | |
{ | |
var response = string.Empty; | |
var lines = stacktrace.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries); | |
foreach (var rawline in lines) | |
{ | |
var line = rawline.Trim(); | |
try | |
{ | |
var newline = "at "; | |
var fullmethod = line.Substring(3, line.IndexOf("(") - 3); | |
var newmethod = ColoredSpan(fullmethod.Substring(fullmethod.LastIndexOf(".")), "darkCyan"); | |
newline += ColoredSpan(fullmethod.Substring(0, fullmethod.LastIndexOf(".")), "darkBlue"); | |
newline += newmethod; | |
try | |
{ | |
var paramstart = line.IndexOf("("); | |
var paramtext = line.Substring(paramstart + 1, line.IndexOf(")") - paramstart - 1); | |
var paramlist = paramtext.Split(','); | |
var paramoutput = string.Empty; | |
if (paramlist.Any(p => !string.IsNullOrEmpty(p))) | |
{ | |
foreach (var paramcomb in paramlist) | |
{ | |
var psplit = paramcomb.Trim().Split(' '); | |
if (!string.IsNullOrEmpty(paramoutput)) | |
{ | |
paramoutput += ", "; | |
} | |
paramoutput += ColoredSpan(psplit[0], "darkCyan") + " " + ColoredSpan(psplit[1], "#666"); | |
} | |
} | |
newline += "(" + paramoutput + ")"; | |
} | |
catch | |
{ | |
} | |
try | |
{ | |
var aftermethod = line.Substring(line.IndexOf(")") + 1); | |
if (!string.IsNullOrWhiteSpace(aftermethod)) | |
{ | |
var filelocation = aftermethod.Substring(4); | |
newline += " " + ColoredSpan(filelocation, "darkMagenta"); | |
} | |
} | |
catch | |
{ | |
} | |
response += newline + "<br />"; | |
} | |
catch | |
{ | |
response += rawline + "<br />"; | |
} | |
} | |
if (response.EndsWith("<br />")) | |
{ | |
response = response.Remove(response.Length - 6); | |
} | |
return response; | |
} | |
catch | |
{ | |
return stacktrace; | |
} | |
} | |
public static string ColoredSpan(string text, string colour) | |
{ | |
return "<span style=\"color:{1}\">{0}</span>".With(text, colour); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment