77#if NET45 || NET40
88using System . Runtime . Remoting . Messaging ;
99#endif
10- using System . Security ;
11- using System . Text ;
12- using System . Threading . Tasks ;
1310using StackifyLib ;
1411using System . Diagnostics ;
1512using StackifyLib . Internal . Logs ;
@@ -31,6 +28,8 @@ public class StackifyTarget : TargetWithLayout
3128 public string callContextKeys { get ; set ; }
3229 public bool ? logMethodNames { get ; set ; }
3330 public bool ? logAllParams { get ; set ; }
31+ public bool ? logAllProperties { get ; set ; }
32+ public bool ? logLastParameter { get ; set ; }
3433
3534 private List < string > _GlobalContextKeys = new List < string > ( ) ;
3635 private List < string > _MappedContextKeys = new List < string > ( ) ;
@@ -99,32 +98,26 @@ protected override void Write(LogEventInfo logEvent)
9998 {
10099 StackifyAPILogger . Log ( ex . ToString ( ) ) ;
101100 }
102-
103101 }
104102
105103
106104 private Dictionary < string , object > GetDiagnosticContextProperties ( )
107105 {
108-
109-
110106 Dictionary < string , object > properties = new Dictionary < string , object > ( ) ;
111107
112-
113108 string ndc = NLog . NestedDiagnosticsContext . TopMessage ;
114109
115110 if ( ! String . IsNullOrEmpty ( ndc ) )
116111 {
117112 properties [ "ndc" ] = ndc ;
118113 }
119114
120-
121115 if ( ! _HasContextKeys )
122116 {
123117 return properties ;
124118 }
125119
126120 // GlobalDiagnosticsContext
127-
128121 foreach ( string gdcKey in _GlobalContextKeys )
129122 {
130123 if ( NLog . GlobalDiagnosticsContext . Contains ( gdcKey ) )
@@ -137,8 +130,8 @@ private Dictionary<string, object> GetDiagnosticContextProperties()
137130 }
138131 }
139132 }
140- // MappedDiagnosticsContext
141133
134+ // MappedDiagnosticsContext
142135 foreach ( string mdcKey in _MappedContextKeys )
143136 {
144137 if ( NLog . MappedDiagnosticsContext . Contains ( mdcKey ) )
@@ -170,7 +163,6 @@ private Dictionary<string, object> GetDiagnosticContextProperties()
170163
171164 internal LogMsg Translate ( LogEventInfo loggingEvent )
172165 {
173-
174166 if ( loggingEvent == null )
175167 return null ;
176168
@@ -180,14 +172,11 @@ internal LogMsg Translate(LogEventInfo loggingEvent)
180172
181173 StackifyLib . Models . LogMsg msg = new LogMsg ( ) ;
182174
183-
184175 if ( loggingEvent . Level != null )
185176 {
186177 msg . Level = loggingEvent . Level . Name ;
187178 }
188179
189-
190-
191180 if ( loggingEvent . HasStackTrace && loggingEvent . UserStackFrame != null )
192181 {
193182 var frame = loggingEvent . UserStackFrame ;
@@ -201,10 +190,8 @@ internal LogMsg Translate(LogEventInfo loggingEvent)
201190 msg . SrcLine = frame . GetFileLineNumber ( ) ;
202191 }
203192 }
204-
205193 }
206194
207-
208195 //if it wasn't set above for some reason we will do it this way as a fallback
209196 if ( string . IsNullOrEmpty ( msg . SrcMethod ) )
210197 {
@@ -240,48 +227,62 @@ internal LogMsg Translate(LogEventInfo loggingEvent)
240227 msg . Msg = ( formattedMessage ?? "" ) . Trim ( ) ;
241228
242229 object debugObject = null ;
243- Dictionary < string , object > args = new Dictionary < string , object > ( ) ;
244230
245- if ( ( loggingEvent . Parameters != null ) && ( loggingEvent . Parameters . Length > 0 ) )
231+ if ( ( logAllProperties ?? true ) && loggingEvent . Properties . Count > 0 )
246232 {
247-
248- for ( int i = 0 ; i < loggingEvent . Parameters . Length ; i ++ )
233+ Dictionary < string , object > args = new Dictionary < string , object > ( ) ;
234+ foreach ( KeyValuePair < object , object > eventProperty in loggingEvent . Properties )
249235 {
250- var item = loggingEvent . Parameters [ i ] ;
251-
252- if ( item == null )
236+ string propertyKey = eventProperty . Key . ToString ( ) ;
237+ if ( ! string . IsNullOrEmpty ( propertyKey ) )
253238 {
254- continue ;
239+ args [ propertyKey ] = eventProperty . Value ;
255240 }
256- else if ( item is Exception )
241+ }
242+ debugObject = args ;
243+ }
244+ else if ( loggingEvent . Parameters != null && loggingEvent . Parameters . Length > 0 )
245+ {
246+ Dictionary < string , object > args = ( logAllParams ?? true ) ? new Dictionary < string , object > ( ) : null ;
247+ if ( args != null || ( logLastParameter ?? true ) )
248+ {
249+ for ( int i = 0 ; i < loggingEvent . Parameters . Length ; i ++ )
257250 {
258- if ( loggingEvent . Exception == null )
251+ var item = loggingEvent . Parameters [ i ] ;
252+
253+ if ( item == null )
259254 {
260- loggingEvent . Exception = ( Exception ) item ;
255+ continue ;
256+ }
257+ else if ( item is Exception )
258+ {
259+ if ( loggingEvent . Exception == null )
260+ {
261+ loggingEvent . Exception = ( Exception ) item ;
262+ }
263+ }
264+ else if ( item . ToString ( ) == msg . Msg )
265+ {
266+ //ignore it.
267+ }
268+ else if ( args != null )
269+ {
270+ args [ "arg" + i ] = loggingEvent . Parameters [ i ] ;
271+ debugObject = item ;
272+ }
273+ else
274+ {
275+ debugObject = item ;
261276 }
262277 }
263- else if ( item . ToString ( ) == msg . Msg )
264- {
265- //ignore it.
266- }
267- else if ( logAllParams ?? true )
268- {
269- args [ "arg" + i ] = loggingEvent . Parameters [ i ] ;
270- debugObject = item ;
271- }
272- else
278+
279+ if ( args != null && args . Count > 1 )
273280 {
274- debugObject = item ;
281+ debugObject = args ;
275282 }
276283 }
277-
278- if ( ( logAllParams ?? true ) && args != null && args . Count > 1 )
279- {
280- debugObject = args ;
281- }
282284 }
283285
284-
285286 StackifyError error = null ;
286287
287288 if ( loggingEvent . Exception != null && loggingEvent . Exception is StackifyError )
@@ -300,13 +301,6 @@ internal LogMsg Translate(LogEventInfo loggingEvent)
300301 diags . Remove ( "transid" ) ;
301302 }
302303
303-
304-
305-
306-
307-
308-
309-
310304 if ( debugObject != null )
311305 {
312306 msg . data = StackifyLib . Utils . HelperFunctions . SerializeDebugData ( debugObject , true , diags ) ;
@@ -315,7 +309,6 @@ internal LogMsg Translate(LogEventInfo loggingEvent)
315309 {
316310 msg . data = StackifyLib . Utils . HelperFunctions . SerializeDebugData ( null , false , diags ) ;
317311 }
318-
319312
320313 if ( msg . Msg != null && error != null )
321314 {
@@ -357,11 +350,8 @@ internal LogMsg Translate(LogEventInfo loggingEvent)
357350 msg . Msg += " #errorgoverned" ;
358351 }
359352
360-
361353 return msg ;
362354 }
363355
364-
365-
366356 }
367357}
0 commit comments