wasSharpNET – Blame information for rev 20

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 {
20 office 18 public static string PrettyPrint(this Exception ex)
16 office 19 {
20 office 20 if(ex == null)
21 return string.Empty;
22  
23 var st = new StackTrace(ex, true);
16 office 24 var frames = st.GetFrames();
20 office 25 if (frames == null)
26 return string.Empty;
16 office 27  
20 office 28 var sb = new StringBuilder();
29 sb.Append(Environment.NewLine);
16 office 30  
31 foreach (var frame in frames)
32 {
20 office 33 var fileName = frame?.GetFileName();
34 if (string.IsNullOrEmpty(fileName))
16 office 35 continue;
36  
20 office 37 sb.Append(Enumerable
38 .Repeat('-', System.Console.WindowWidth)
39 .Concat(Environment.NewLine)
40 .ToArray());
16 office 41 sb.Append("File: ");
20 office 42 sb.Append(fileName);
16 office 43 sb.Append(@" Method: ");
44 sb.Append(frame.GetMethod().Name);
45 sb.Append(@" Line and Column : ");
46 sb.Append(frame.GetFileLineNumber());
47 sb.Append(@":");
48 sb.Append(frame.GetFileColumnNumber());
17 office 49 sb.Append(Environment.NewLine);
16 office 50 }
51  
20 office 52 sb.Append(Enumerable
53 .Repeat('-', System.Console.WindowWidth)
54 .Concat(Environment.NewLine)
55 .ToArray());
56 sb.Append(ex);
57 sb.Append(Environment.NewLine);
58 sb.Append(Enumerable
59 .Repeat('-', System.Console.WindowWidth)
60 .Concat(Environment.NewLine)
61 .ToArray());
16 office 62  
63 return sb.ToString();
64 }
65 }
66 }