Guest User

Untitled

a guest
Apr 27th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.02 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace PrettifyStackTrace
  5. {
  6. public class Stacky
  7. {
  8. public string ExceptionType { get; set; }
  9. public string Method { get; set; }
  10. public string FileName { get; set; }
  11. public int Line { get; set; }
  12. public List<string> StackLines { get; set; }
  13. }
  14.  
  15. public static class Helpers
  16. {
  17. public static Stacky PrettifyStackTrace(string stackTrace, Type exceptionType)
  18. {
  19. var stacky = new Stacky();
  20. stacky.ExceptionType = exceptionType.ToString();
  21.  
  22. if (!string.IsNullOrEmpty(stackTrace))
  23. {
  24. stacky.StackLines = new List<string>();
  25.  
  26. var lines = stackTrace.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
  27.  
  28. var stackCount = 0;
  29.  
  30. try // Try to Prettify
  31. {
  32. for (int i = 0; i < lines.Length; i++)
  33. {
  34. var line = lines[i];
  35.  
  36. if (i == 0)
  37. {
  38. var subStrings = line.Split(new string[] { " in " }, StringSplitOptions.RemoveEmptyEntries);
  39. var methodStrings = subStrings[0].Split(new string[] { " at " }, StringSplitOptions.RemoveEmptyEntries);
  40. var fileStrings = (subStrings.Length > 1) ? subStrings[1].Split(new string[] { ":line " }, StringSplitOptions.RemoveEmptyEntries) : new string[] { subStrings[0], string.Empty };
  41. stacky.Method = methodStrings[methodStrings.Length - 1];
  42. stacky.FileName = fileStrings[0].Contains(".cs") ? fileStrings[0] : "System/NET Exception";
  43. stacky.Line = int.Parse(fileStrings[1]);
  44.  
  45. stacky.StackLines.Add(stacky.Method);
  46. }
  47. else if (line.StartsWith("---"))
  48. {
  49. stackCount++;
  50. stacky.StackLines.Add($"=== Sub-stack {stackCount} ===");
  51. }
  52. else
  53. {
  54. stacky.StackLines.Add(line.Replace(" at ", " @ "));
  55. }
  56. }
  57. }
  58. catch // Else just print the lines as is.
  59. {
  60. stackCount = 0;
  61. stacky.StackLines.Clear();
  62.  
  63. for (int i = 0; i < lines.Length; i++)
  64. {
  65. var line = lines[i];
  66.  
  67. if (line.StartsWith("---"))
  68. {
  69. stackCount++;
  70. stacky.StackLines.Add($"=== Sub-stack {stackCount} ===");
  71. }
  72. else
  73. { stacky.StackLines.Add(line.Replace(" at ", " @ ")); }
  74. }
  75. }
  76. }
  77.  
  78. return stacky;
  79. }
  80. }
  81. }
Add Comment
Please, Sign In to add comment