Skip to content

Commit a0188c3

Browse files
committed
Merge pull request #4 from AdamCLarsen/fix-Log4netContextStack
Add handing for Log4Net Thread Context stacks
2 parents e1f1dec + d189cf4 commit a0188c3

File tree

2 files changed

+52
-4
lines changed

2 files changed

+52
-4
lines changed

Src/StackifyLib.log4net.v1_2_10/StackifyAppender.cs

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

317317
if (mdcValue != null)
318318
{
319-
properties[mdcKey.ToLower()] = mdcValue;
319+
// Check if the property is a Log4net Context Stack,
320+
// If it is, then we want to read it as a string as that is
321+
// the expected behavior for a Log4Net Appender.
322+
var tcs = mdcValue as Apache_log4net.Util.ThreadContextStack;
323+
if (tcs != null)
324+
{
325+
properties[mdcKey.ToLower()] = tcs.ToString();
326+
}
327+
else
328+
{
329+
// for anything else, we will let Json.Net take care of it.
330+
properties[mdcKey.ToLower()] = mdcValue;
331+
}
320332
}
321333
}
322334

@@ -326,7 +338,19 @@ private Dictionary<string, object> GetDiagnosticContextProperties()
326338

327339
if (mdcValue != null)
328340
{
329-
properties[mdcKey.ToLower()] = mdcValue;
341+
// Check if the property is a Log4net Context Stack,
342+
// If it is, then we want to read it as a string as that is
343+
// the expected behavior for a Log4Net Appender.
344+
var tcs = mdcValue as Apache_log4net.Util.ThreadContextStack;
345+
if (tcs != null)
346+
{
347+
properties[mdcKey.ToLower()] = tcs.ToString();
348+
}
349+
else
350+
{
351+
// for anything else, we will let Json.Net take care of it.
352+
properties[mdcKey.ToLower()] = mdcValue;
353+
}
330354
}
331355
}
332356

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)