Last active
December 6, 2021 04:03
-
-
Save houseofcat/2bf0c43c8382f56a0c5779fec13332d7 to your computer and use it in GitHub Desktop.
C# Prettify Stack Trace
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
using System; | |
using System.Collections.Generic; | |
namespace PrettifyStackTrace | |
{ | |
public class Stacky | |
{ | |
public string ExceptionType { get; set; } | |
public string Method { get; set; } | |
public string FileName { get; set; } | |
public int Line { get; set; } | |
public List<string> StackLines { get; set; } | |
} | |
public static class Helpers | |
{ | |
public static Stacky PrettifyStackTrace(string stackTrace, Type exceptionType) | |
{ | |
var stacky = new Stacky(); | |
stacky.ExceptionType = exceptionType.ToString(); | |
if (!string.IsNullOrEmpty(stackTrace)) | |
{ | |
stacky.StackLines = new List<string>(); | |
var lines = stackTrace.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries); | |
var stackCount = 0; | |
try // Try to Prettify | |
{ | |
for (int i = 0; i < lines.Length; i++) | |
{ | |
var line = lines[i]; | |
if (i == 0) | |
{ | |
var subStrings = line.Split(new string[] { " in " }, StringSplitOptions.RemoveEmptyEntries); | |
var methodStrings = subStrings[0].Split(new string[] { " at " }, StringSplitOptions.RemoveEmptyEntries); | |
var fileStrings = (subStrings.Length > 1) ? subStrings[1].Split(new string[] { ":line " }, StringSplitOptions.RemoveEmptyEntries) : new string[] { subStrings[0], string.Empty }; | |
stacky.Method = methodStrings[methodStrings.Length - 1]; | |
stacky.FileName = fileStrings[0].Contains(".cs") ? fileStrings[0] : "System/NET Exception"; | |
stacky.Line = int.Parse(fileStrings[1]); | |
stacky.StackLines.Add(stacky.Method); | |
} | |
else if (line.StartsWith("---")) | |
{ | |
stackCount++; | |
stacky.StackLines.Add($"=== Sub-stack {stackCount} ==="); | |
} | |
else | |
{ | |
stacky.StackLines.Add(line.Replace(" at ", " @ ")); | |
} | |
} | |
} | |
catch // Else just print the lines as is. | |
{ | |
stackCount = 0; | |
stacky.StackLines.Clear(); | |
for (int i = 0; i < lines.Length; i++) | |
{ | |
var line = lines[i]; | |
if (line.StartsWith("---")) | |
{ | |
stackCount++; | |
stacky.StackLines.Add($"=== Sub-stack {stackCount} ==="); | |
} | |
else | |
{ stacky.StackLines.Add(line.Replace(" at ", " @ ")); } | |
} | |
} | |
} | |
return stacky; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
And then I refactored
ExceptionPretty
toExceptionSummary
... until a better name can be conjured.