wasSharpNET – Blame information for rev 19

Subversion Repositories:
Rev:
Rev Author Line No. Line
16 office 1 ///////////////////////////////////////////////////////////////////////////
2 // Copyright (C) Wizardry and Steamworks 2016 - License: GNU GPLv3 //
3 // Please see: http://www.gnu.org/licenses/gpl.html for legal details, //
4 // rights of fair usage, the disclaimer and warranty conditions. //
5 ///////////////////////////////////////////////////////////////////////////
6 // Based on Oguzhan KIRCALI & CDspace @ https://stackoverflow.com/questions/4272579/how-to-print-full-stack-trace-in-exception
7  
8 using System;
9 using System.Diagnostics;
10 using System.Linq;
11 using System.Text;
18 office 12 using System.IO;
16 office 13  
14 namespace wasSharpNET.Diagnostics
15 {
16 public static class ExceptionExtensions
17 {
18 public static string PrettyPrint(this Exception x)
19 {
20 var st = new StackTrace(x, true);
21 var frames = st.GetFrames();
22  
23 StringBuilder sb = new StringBuilder();
18 office 24 sb.Append(Enumerable.Repeat('-', 75).ToArray());
16 office 25  
26 int indent = 0;
27 foreach (var frame in frames)
28 {
29 if (frame.GetFileLineNumber() < 1)
30 continue;
31  
18 office 32 sb.Append(Enumerable.Repeat(' ', indent).ToArray());
16 office 33 sb.Append(@" -> ");
34 sb.Append("File: ");
18 office 35 sb.Append(string.Join(
36 Path.DirectorySeparatorChar.ToString(),
37 frame.GetFileName()
38 .Split(Path.DirectorySeparatorChar)
39 .Reverse()
40 .Take(2)
41 .Reverse()
42 )
43 );
16 office 44 sb.Append(@" Method: ");
45 sb.Append(frame.GetMethod().Name);
46 sb.Append(@" Line and Column : ");
47 sb.Append(frame.GetFileLineNumber());
48 sb.Append(@":");
49 sb.Append(frame.GetFileColumnNumber());
17 office 50 sb.Append(Environment.NewLine);
16 office 51  
19 office 52 indent += 1;
16 office 53 }
54  
18 office 55 sb.Append(Enumerable.Repeat('-', 75).ToArray());
16 office 56 sb.Append(x);
18 office 57 sb.Append(Enumerable.Repeat('-', 75).ToArray());
16 office 58  
59 return sb.ToString();
60 }
61 }
62 }