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