1
+ using System ;
2
+ using System . Text ;
3
+
4
+ namespace KY . Core
5
+ {
6
+ public sealed class ExceptionFormatter : ExceptionFormatter < Exception >
7
+ { }
8
+
9
+ public class ExceptionFormatter < T > : IExceptionFormatter
10
+ where T : Exception
11
+ {
12
+ public string Format ( T exception )
13
+ {
14
+ StringBuilder builder = new StringBuilder ( ) ;
15
+ this . FormatMessage ( exception , builder ) ;
16
+ this . FormatStackTrace ( exception , builder ) ;
17
+ this . FormatInnerException ( exception , builder ) ;
18
+ return builder . ToString ( ) ;
19
+ }
20
+
21
+ protected virtual void BeforeFormatMessage ( T exception , StringBuilder builder )
22
+ { }
23
+
24
+ protected virtual void FormatMessage ( T exception , StringBuilder builder )
25
+ {
26
+ this . BeforeFormatMessage ( exception , builder ) ;
27
+ builder . AppendLine ( exception . Message ) ;
28
+ this . AfterFormatMessage ( exception , builder ) ;
29
+ }
30
+
31
+ protected virtual void AfterFormatMessage ( T exception , StringBuilder builder )
32
+ { }
33
+
34
+ protected virtual void BeforeFormatStackTrace ( T exception , StringBuilder builder )
35
+ { }
36
+
37
+ protected virtual void FormatStackTrace ( T exception , StringBuilder builder )
38
+ {
39
+ this . BeforeFormatStackTrace ( exception , builder ) ;
40
+ builder . AppendLine ( exception . StackTrace ) ;
41
+ this . AfterFormatStackTrace ( exception , builder ) ;
42
+ }
43
+
44
+ protected virtual void AfterFormatStackTrace ( T exception , StringBuilder builder )
45
+ { }
46
+
47
+ protected virtual void BeforeFormatInnerException ( T exception , StringBuilder builder )
48
+ { }
49
+
50
+ protected virtual void FormatInnerException ( T exception , StringBuilder builder )
51
+ {
52
+ this . BeforeFormatInnerException ( exception , builder ) ;
53
+ if ( exception . InnerException != null )
54
+ {
55
+ builder . AppendLine ( " === INNER EXCEPTION ===" ) ;
56
+ builder . AppendLine ( $ " { Logger . Extension . Format ( exception . InnerException ) } ") ;
57
+ }
58
+ this . AfterFormatInnerException ( exception , builder ) ;
59
+ }
60
+
61
+ protected virtual void AfterFormatInnerException ( T exception , StringBuilder builder )
62
+ { }
63
+
64
+ string IExceptionFormatter . Format ( Exception exception )
65
+ {
66
+ return this . Format ( ( T ) exception ) ;
67
+ }
68
+ }
69
+ }
0 commit comments