Skip to content

Commit 0641790

Browse files
committed
Add handing for Log4Net Thread Context stacks
1 parent e1f1dec commit 0641790

File tree

1 file changed

+26
-2
lines changed

1 file changed

+26
-2
lines changed

Src/StackifyLib.log4net/StackifyAppender.cs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,19 @@ private Dictionary<string, object> GetDiagnosticContextProperties()
307307

308308
if (mdcValue != null)
309309
{
310-
properties[mdcKey.ToLower()] = mdcValue;
310+
// Check if the property is a Log4net Context Stack,
311+
// If it is, then we want to read it as a string as that is
312+
// the expected behavior for a Log4Net Appender.
313+
var tcs = mdcValue as Apache_log4net.Util.ThreadContextStack;
314+
if (tcs != null)
315+
{
316+
properties[mdcKey.ToLower()] = tcs.ToString();
317+
}
318+
else
319+
{
320+
// for anything else, we will let Json.Net take care of it.
321+
properties[mdcKey.ToLower()] = mdcValue;
322+
}
311323
}
312324
}
313325

@@ -317,7 +329,19 @@ private Dictionary<string, object> GetDiagnosticContextProperties()
317329

318330
if (mdcValue != null)
319331
{
320-
properties[mdcKey.ToLower()] = mdcValue;
332+
// Check if the property is a Log4net Context Stack,
333+
// If it is, then we want to read it as a string as that is
334+
// the expected behavior for a Log4Net Appender.
335+
var tcs = mdcValue as Apache_log4net.Util.ThreadContextStack;
336+
if (tcs != null)
337+
{
338+
properties[mdcKey.ToLower()] = tcs.ToString();
339+
}
340+
else
341+
{
342+
// for anything else, we will let Json.Net take care of it.
343+
properties[mdcKey.ToLower()] = mdcValue;
344+
}
321345
}
322346
}
323347

0 commit comments

Comments
 (0)