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,7 @@ 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 ; }
3432
3533 private List < string > _GlobalContextKeys = new List < string > ( ) ;
3634 private List < string > _MappedContextKeys = new List < string > ( ) ;
@@ -99,32 +97,26 @@ protected override void Write(LogEventInfo logEvent)
9997 {
10098 StackifyAPILogger . Log ( ex . ToString ( ) ) ;
10199 }
102-
103100 }
104101
105102
106103 private Dictionary < string , object > GetDiagnosticContextProperties ( )
107104 {
108-
109-
110105 Dictionary < string , object > properties = new Dictionary < string , object > ( ) ;
111106
112-
113107 string ndc = NLog . NestedDiagnosticsContext . TopMessage ;
114108
115109 if ( ! String . IsNullOrEmpty ( ndc ) )
116110 {
117111 properties [ "ndc" ] = ndc ;
118112 }
119113
120-
121114 if ( ! _HasContextKeys )
122115 {
123116 return properties ;
124117 }
125118
126119 // GlobalDiagnosticsContext
127-
128120 foreach ( string gdcKey in _GlobalContextKeys )
129121 {
130122 if ( NLog . GlobalDiagnosticsContext . Contains ( gdcKey ) )
@@ -137,8 +129,8 @@ private Dictionary<string, object> GetDiagnosticContextProperties()
137129 }
138130 }
139131 }
140- // MappedDiagnosticsContext
141132
133+ // MappedDiagnosticsContext
142134 foreach ( string mdcKey in _MappedContextKeys )
143135 {
144136 if ( NLog . MappedDiagnosticsContext . Contains ( mdcKey ) )
@@ -150,6 +142,20 @@ private Dictionary<string, object> GetDiagnosticContextProperties()
150142 properties . Add ( mdcKey . ToLower ( ) , mdcValue ) ;
151143 }
152144 }
145+ }
146+
147+ // MappedDiagnosticsLogicalContext (Async CallContext)
148+ foreach ( string mdlcKey in _MappedContextKeys )
149+ {
150+ if ( NLog . MappedDiagnosticsLogicalContext . Contains ( mdlcKey ) )
151+ {
152+ string mdcValue = NLog . MappedDiagnosticsLogicalContext . Get ( mdlcKey ) ;
153+
154+ if ( mdcValue != null )
155+ {
156+ properties . Add ( mdlcKey . ToLower ( ) , mdcValue ) ;
157+ }
158+ }
153159 }
154160
155161#if NET45 || NET40
@@ -170,7 +176,6 @@ private Dictionary<string, object> GetDiagnosticContextProperties()
170176
171177 internal LogMsg Translate ( LogEventInfo loggingEvent )
172178 {
173-
174179 if ( loggingEvent == null )
175180 return null ;
176181
@@ -180,14 +185,11 @@ internal LogMsg Translate(LogEventInfo loggingEvent)
180185
181186 StackifyLib . Models . LogMsg msg = new LogMsg ( ) ;
182187
183-
184188 if ( loggingEvent . Level != null )
185189 {
186190 msg . Level = loggingEvent . Level . Name ;
187191 }
188192
189-
190-
191193 if ( loggingEvent . HasStackTrace && loggingEvent . UserStackFrame != null )
192194 {
193195 var frame = loggingEvent . UserStackFrame ;
@@ -201,10 +203,8 @@ internal LogMsg Translate(LogEventInfo loggingEvent)
201203 msg . SrcLine = frame . GetFileLineNumber ( ) ;
202204 }
203205 }
204-
205206 }
206207
207-
208208 //if it wasn't set above for some reason we will do it this way as a fallback
209209 if ( string . IsNullOrEmpty ( msg . SrcMethod ) )
210210 {
@@ -294,19 +294,29 @@ internal LogMsg Translate(LogEventInfo loggingEvent)
294294 }
295295
296296 var diags = GetDiagnosticContextProperties ( ) ;
297+
298+ if ( logAllProperties ?? true )
299+ {
300+ if ( loggingEvent . Properties . Count > 0 )
301+ {
302+ foreach ( KeyValuePair < object , object > eventProperty in loggingEvent . Properties )
303+ {
304+ string propertyKey = eventProperty . Key . ToString ( ) ;
305+ if ( ! string . IsNullOrEmpty ( propertyKey ) )
306+ {
307+ string propertyValue = eventProperty . Value != null ? Convert . ToString ( eventProperty . Value , loggingEvent . FormatProvider ?? CultureInfo . CurrentCulture ) : null ;
308+ diags [ propertyKey . ToLower ( ) ] = propertyValue ;
309+ }
310+ }
311+ }
312+ }
313+
297314 if ( diags != null && diags . ContainsKey ( "transid" ) )
298315 {
299316 msg . TransID = diags [ "transid" ] . ToString ( ) ;
300317 diags . Remove ( "transid" ) ;
301318 }
302319
303-
304-
305-
306-
307-
308-
309-
310320 if ( debugObject != null )
311321 {
312322 msg . data = StackifyLib . Utils . HelperFunctions . SerializeDebugData ( debugObject , true , diags ) ;
@@ -315,7 +325,6 @@ internal LogMsg Translate(LogEventInfo loggingEvent)
315325 {
316326 msg . data = StackifyLib . Utils . HelperFunctions . SerializeDebugData ( null , false , diags ) ;
317327 }
318-
319328
320329 if ( msg . Msg != null && error != null )
321330 {
@@ -357,11 +366,8 @@ internal LogMsg Translate(LogEventInfo loggingEvent)
357366 msg . Msg += " #errorgoverned" ;
358367 }
359368
360-
361369 return msg ;
362370 }
363371
364-
365-
366372 }
367373}
0 commit comments