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,47 +227,33 @@ 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 ;
255- }
256- else if ( item is Exception )
257- {
258- if ( loggingEvent . Exception == null )
259- {
260- loggingEvent . Exception = ( Exception ) item ;
261- }
262- }
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
273- {
274- debugObject = item ;
239+ args [ propertyKey ] = eventProperty . Value ;
275240 }
276241 }
277242
278- if ( ( logAllParams ?? true ) && args != null && args . Count > 1 )
243+ if ( ( logAllParams ?? false ) && loggingEvent . Parameters != null && loggingEvent . Parameters . Length > 0 )
244+ {
245+ debugObject = CaptureParameters ( loggingEvent , msg . Msg , args ) ;
246+ }
247+ else
279248 {
280249 debugObject = args ;
281250 }
282251 }
283-
252+ else if ( loggingEvent . Parameters != null && loggingEvent . Parameters . Length > 0 )
253+ {
254+ Dictionary < string , object > args = ( logAllParams ?? true ) ? new Dictionary < string , object > ( ) : null ;
255+ debugObject = CaptureParameters ( loggingEvent , msg . Msg , args ) ;
256+ }
284257
285258 StackifyError error = null ;
286259
@@ -300,13 +273,6 @@ internal LogMsg Translate(LogEventInfo loggingEvent)
300273 diags . Remove ( "transid" ) ;
301274 }
302275
303-
304-
305-
306-
307-
308-
309-
310276 if ( debugObject != null )
311277 {
312278 msg . data = StackifyLib . Utils . HelperFunctions . SerializeDebugData ( debugObject , true , diags ) ;
@@ -315,7 +281,6 @@ internal LogMsg Translate(LogEventInfo loggingEvent)
315281 {
316282 msg . data = StackifyLib . Utils . HelperFunctions . SerializeDebugData ( null , false , diags ) ;
317283 }
318-
319284
320285 if ( msg . Msg != null && error != null )
321286 {
@@ -357,11 +322,51 @@ internal LogMsg Translate(LogEventInfo loggingEvent)
357322 msg . Msg += " #errorgoverned" ;
358323 }
359324
360-
361325 return msg ;
362326 }
363327
328+ private object CaptureParameters ( LogEventInfo loggingEvent , string logMessage , Dictionary < string , object > args )
329+ {
330+ object debugObject = null ;
331+ if ( args != null || ( logLastParameter ?? true ) )
332+ {
333+ for ( int i = 0 ; i < loggingEvent . Parameters . Length ; i ++ )
334+ {
335+ var item = loggingEvent . Parameters [ i ] ;
336+
337+ if ( item == null )
338+ {
339+ continue ;
340+ }
341+ else if ( item is Exception )
342+ {
343+ if ( loggingEvent . Exception == null )
344+ {
345+ loggingEvent . Exception = ( Exception ) item ;
346+ }
347+ }
348+ else if ( item . ToString ( ) == logMessage )
349+ {
350+ //ignore it.
351+ }
352+ else if ( args != null )
353+ {
354+ args [ "arg" + i ] = loggingEvent . Parameters [ i ] ;
355+ debugObject = item ;
356+ }
357+ else
358+ {
359+ debugObject = item ;
360+ }
361+ }
362+
363+ if ( args != null && args . Count > 1 )
364+ {
365+ debugObject = args ;
366+ }
367+ }
364368
365-
369+ return debugObject ;
370+ }
366371 }
367372}
0 commit comments