5
5
using NLog . Config ;
6
6
using NLog . Targets ;
7
7
using Flow . Launcher . Infrastructure . UserSettings ;
8
+ using JetBrains . Annotations ;
9
+ using NLog . Fluent ;
10
+ using NLog . Targets . Wrappers ;
11
+ using System . Runtime . ExceptionServices ;
12
+ using System . Text ;
8
13
9
14
namespace Flow . Launcher . Infrastructure . Logger
10
15
{
@@ -23,23 +28,44 @@ static Log()
23
28
}
24
29
25
30
var configuration = new LoggingConfiguration ( ) ;
26
- var target = new FileTarget ( ) ;
27
- configuration . AddTarget ( "file" , target ) ;
28
- target . FileName = CurrentLogDirectory . Replace ( @"\" , "/" ) + "/${shortdate}.txt" ;
31
+
32
+ const string layout =
33
+ @"${date:format=HH\:mm\:ss.ffffK} - " +
34
+ @"${level:uppercase=true:padding=-5} - ${logger} - ${message:l}" +
35
+ @"${onexception:${newline}" +
36
+ @"EXCEPTION OCCURS\: ${exception:format=tostring}${newline}}" ;
37
+
38
+ var fileTarget = new FileTarget
39
+ {
40
+ FileName = CurrentLogDirectory . Replace ( @"\" , "/" ) + "/${shortdate}.txt" ,
41
+ Layout = layout
42
+ } ;
43
+
44
+ var fileTargetASyncWrapper = new AsyncTargetWrapper ( fileTarget ) ;
45
+
46
+ var debugTarget = new OutputDebugStringTarget
47
+ {
48
+ Layout = layout
49
+ } ;
50
+
51
+ configuration . AddTarget ( "file" , fileTargetASyncWrapper ) ;
52
+ configuration . AddTarget ( "debug" , debugTarget ) ;
53
+
29
54
#if DEBUG
30
- var rule = new LoggingRule ( "*" , LogLevel . Debug , target ) ;
55
+ var fileRule = new LoggingRule ( "*" , LogLevel . Debug , fileTargetASyncWrapper ) ;
56
+ var debugRule = new LoggingRule ( "*" , LogLevel . Debug , debugTarget ) ;
57
+ configuration . LoggingRules . Add ( debugRule ) ;
31
58
#else
32
- var rule = new LoggingRule ( "*" , LogLevel . Info , target ) ;
59
+ var fileRule = new LoggingRule ( "*" , LogLevel . Info , fileTargetASyncWrapper ) ;
33
60
#endif
34
- configuration . LoggingRules . Add ( rule ) ;
61
+ configuration . LoggingRules . Add ( fileRule ) ;
35
62
LogManager . Configuration = configuration ;
36
63
}
37
64
38
65
private static void LogFaultyFormat ( string message )
39
66
{
40
67
var logger = LogManager . GetLogger ( "FaultyLogger" ) ;
41
68
message = $ "Wrong logger message format <{ message } >";
42
- System . Diagnostics . Debug . WriteLine ( $ "FATAL|{ message } ") ;
43
69
logger . Fatal ( message ) ;
44
70
}
45
71
@@ -51,12 +77,11 @@ private static bool FormatValid(string message)
51
77
}
52
78
53
79
54
-
55
- [ MethodImpl ( MethodImplOptions . Synchronized ) ]
56
80
public static void Exception ( string className , string message , System . Exception exception , [ CallerMemberName ] string methodName = "" )
57
81
{
82
+ exception = exception . Demystify ( ) ;
58
83
#if DEBUG
59
- throw exception ;
84
+ ExceptionDispatchInfo . Capture ( exception ) . Throw ( ) ;
60
85
#else
61
86
var classNameWithMethod = CheckClassAndMessageAndReturnFullClassWithMethod ( className , message , methodName ) ;
62
87
@@ -90,23 +115,9 @@ private static void ExceptionInternal(string classAndMethod, string message, Sys
90
115
{
91
116
var logger = LogManager . GetLogger ( classAndMethod ) ;
92
117
93
- System . Diagnostics . Debug . WriteLine ( $ "ERROR|{ message } ") ;
94
-
95
- logger . Error ( "-------------------------- Begin exception --------------------------" ) ;
96
- logger . Error ( message ) ;
118
+ var messageBuilder = new StringBuilder ( ) ;
97
119
98
- do
99
- {
100
- logger . Error ( $ "Exception full name:\n <{ e . GetType ( ) . FullName } >") ;
101
- logger . Error ( $ "Exception message:\n <{ e . Message } >") ;
102
- logger . Error ( $ "Exception stack trace:\n <{ e . StackTrace } >") ;
103
- logger . Error ( $ "Exception source:\n <{ e . Source } >") ;
104
- logger . Error ( $ "Exception target site:\n <{ e . TargetSite } >") ;
105
- logger . Error ( $ "Exception HResult:\n <{ e . HResult } >") ;
106
- e = e . InnerException ;
107
- } while ( e != null ) ;
108
-
109
- logger . Error ( "-------------------------- End exception --------------------------" ) ;
120
+ logger . Error ( e , message ) ;
110
121
}
111
122
112
123
private static void LogInternal ( string message , LogLevel level )
@@ -117,8 +128,6 @@ private static void LogInternal(string message, LogLevel level)
117
128
var prefix = parts [ 1 ] ;
118
129
var unprefixed = parts [ 2 ] ;
119
130
var logger = LogManager . GetLogger ( prefix ) ;
120
-
121
- System . Diagnostics . Debug . WriteLine ( $ "{ level . Name } |{ message } ") ;
122
131
logger . Log ( level , unprefixed ) ;
123
132
}
124
133
else
@@ -128,11 +137,12 @@ private static void LogInternal(string message, LogLevel level)
128
137
}
129
138
130
139
/// <param name="message">example: "|prefix|unprefixed" </param>
131
- [ MethodImpl ( MethodImplOptions . Synchronized ) ]
140
+ /// <param name="e">Exception</param>
132
141
public static void Exception ( string message , System . Exception e )
133
142
{
143
+ e = e . Demystify ( ) ;
134
144
#if DEBUG
135
- throw e ;
145
+ ExceptionDispatchInfo . Capture ( e ) . Throw ( ) ;
136
146
#else
137
147
if ( FormatValid ( message ) )
138
148
{
@@ -165,7 +175,6 @@ private static void LogInternal(LogLevel level, string className, string message
165
175
166
176
var logger = LogManager . GetLogger ( classNameWithMethod ) ;
167
177
168
- System . Diagnostics . Debug . WriteLine ( $ "{ level . Name } |{ message } ") ;
169
178
logger . Log ( level , message ) ;
170
179
}
171
180
0 commit comments