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,63 @@ 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+
245+ if ( loggingEvent . Parameters != null && loggingEvent . Parameters . Length > 0 && ( debugObject == null || logAllParams == true ) )
246+ {
247+ Dictionary < string , object > args = ( logAllParams ?? true ) ? new Dictionary < string , object > ( ) : null ;
248+ if ( args != null || ( logLastParameter ?? true ) )
249+ {
250+ for ( int i = 0 ; i < loggingEvent . Parameters . Length ; i ++ )
257251 {
258- if ( loggingEvent . Exception == null )
252+ var item = loggingEvent . Parameters [ i ] ;
253+
254+ if ( item == null )
259255 {
260- loggingEvent . Exception = ( Exception ) item ;
256+ continue ;
257+ }
258+ else if ( item is Exception )
259+ {
260+ if ( loggingEvent . Exception == null )
261+ {
262+ loggingEvent . Exception = ( Exception ) item ;
263+ }
264+ }
265+ else if ( item . ToString ( ) == msg . Msg )
266+ {
267+ //ignore it.
268+ }
269+ else if ( args != null )
270+ {
271+ args [ "arg" + i ] = loggingEvent . Parameters [ i ] ;
272+ debugObject = item ;
273+ }
274+ else
275+ {
276+ debugObject = item ;
261277 }
262278 }
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
279+
280+ if ( args != null && args . Count > 1 )
273281 {
274- debugObject = item ;
282+ debugObject = args ;
275283 }
276284 }
277-
278- if ( ( logAllParams ?? true ) && args != null && args . Count > 1 )
279- {
280- debugObject = args ;
281- }
282285 }
283286
284-
285287 StackifyError error = null ;
286288
287289 if ( loggingEvent . Exception != null && loggingEvent . Exception is StackifyError )
@@ -300,13 +302,6 @@ internal LogMsg Translate(LogEventInfo loggingEvent)
300302 diags . Remove ( "transid" ) ;
301303 }
302304
303-
304-
305-
306-
307-
308-
309-
310305 if ( debugObject != null )
311306 {
312307 msg . data = StackifyLib . Utils . HelperFunctions . SerializeDebugData ( debugObject , true , diags ) ;
@@ -315,7 +310,6 @@ internal LogMsg Translate(LogEventInfo loggingEvent)
315310 {
316311 msg . data = StackifyLib . Utils . HelperFunctions . SerializeDebugData ( null , false , diags ) ;
317312 }
318-
319313
320314 if ( msg . Msg != null && error != null )
321315 {
@@ -357,11 +351,8 @@ internal LogMsg Translate(LogEventInfo loggingEvent)
357351 msg . Msg += " #errorgoverned" ;
358352 }
359353
360-
361354 return msg ;
362355 }
363356
364-
365-
366357 }
367358}
0 commit comments