55using NLog . Config ;
66using NLog . Targets ;
77using 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 ;
813
914namespace Flow . Launcher . Infrastructure . Logger
1015{
@@ -23,23 +28,44 @@ static Log()
2328 }
2429
2530 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+
2954#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 ) ;
3158#else
32- var rule = new LoggingRule ( "*" , LogLevel . Info , target ) ;
59+ var fileRule = new LoggingRule ( "*" , LogLevel . Info , fileTargetASyncWrapper ) ;
3360#endif
34- configuration . LoggingRules . Add ( rule ) ;
61+ configuration . LoggingRules . Add ( fileRule ) ;
3562 LogManager . Configuration = configuration ;
3663 }
3764
3865 private static void LogFaultyFormat ( string message )
3966 {
4067 var logger = LogManager . GetLogger ( "FaultyLogger" ) ;
4168 message = $ "Wrong logger message format <{ message } >";
42- System . Diagnostics . Debug . WriteLine ( $ "FATAL|{ message } ") ;
4369 logger . Fatal ( message ) ;
4470 }
4571
@@ -51,12 +77,11 @@ private static bool FormatValid(string message)
5177 }
5278
5379
54-
55- [ MethodImpl ( MethodImplOptions . Synchronized ) ]
5680 public static void Exception ( string className , string message , System . Exception exception , [ CallerMemberName ] string methodName = "" )
5781 {
82+ exception = exception . Demystify ( ) ;
5883#if DEBUG
59- throw exception ;
84+ ExceptionDispatchInfo . Capture ( exception ) . Throw ( ) ;
6085#else
6186 var classNameWithMethod = CheckClassAndMessageAndReturnFullClassWithMethod ( className , message , methodName ) ;
6287
@@ -90,23 +115,9 @@ private static void ExceptionInternal(string classAndMethod, string message, Sys
90115 {
91116 var logger = LogManager . GetLogger ( classAndMethod ) ;
92117
93- System . Diagnostics . Debug . WriteLine ( $ "ERROR|{ message } ") ;
94-
95- logger . Error ( "-------------------------- Begin exception --------------------------" ) ;
96- logger . Error ( message ) ;
118+ var messageBuilder = new StringBuilder ( ) ;
97119
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 ) ;
110121 }
111122
112123 private static void LogInternal ( string message , LogLevel level )
@@ -117,8 +128,6 @@ private static void LogInternal(string message, LogLevel level)
117128 var prefix = parts [ 1 ] ;
118129 var unprefixed = parts [ 2 ] ;
119130 var logger = LogManager . GetLogger ( prefix ) ;
120-
121- System . Diagnostics . Debug . WriteLine ( $ "{ level . Name } |{ message } ") ;
122131 logger . Log ( level , unprefixed ) ;
123132 }
124133 else
@@ -128,11 +137,12 @@ private static void LogInternal(string message, LogLevel level)
128137 }
129138
130139 /// <param name="message">example: "|prefix|unprefixed" </param>
131- [ MethodImpl ( MethodImplOptions . Synchronized ) ]
140+ /// <param name="e">Exception</param>
132141 public static void Exception ( string message , System . Exception e )
133142 {
143+ e = e . Demystify ( ) ;
134144#if DEBUG
135- throw e ;
145+ ExceptionDispatchInfo . Capture ( e ) . Throw ( ) ;
136146#else
137147 if ( FormatValid ( message ) )
138148 {
@@ -165,7 +175,6 @@ private static void LogInternal(LogLevel level, string className, string message
165175
166176 var logger = LogManager . GetLogger ( classNameWithMethod ) ;
167177
168- System . Diagnostics . Debug . WriteLine ( $ "{ level . Name } |{ message } ") ;
169178 logger . Log ( level , message ) ;
170179 }
171180
0 commit comments