Skip to content

Commit

Permalink
Merge pull request #3316 from NLog/fix-serialize-mdc-mdlc-targetwithc…
Browse files Browse the repository at this point in the history
…ontext

TargetWithContext - serialize MDC and MDLC values properly
  • Loading branch information
repo-ranger[bot] committed Apr 16, 2019
2 parents f84e5d9 + 8e57da2 commit 033da99
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 14 deletions.
4 changes: 2 additions & 2 deletions src/NLog/Targets/TargetWithContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ private bool TryGetContextPropertyValue(LogEventInfo logEvent, TargetPropertyWit
object value = MappedDiagnosticsContext.GetObject(name);
if (SerializeMdcItem(logEvent, name, value, out var serializedValue))
{
AddContextProperty(logEvent, name, value, checkForDuplicates, contextProperties);
AddContextProperty(logEvent, name, serializedValue, checkForDuplicates, contextProperties);
}
}
return contextProperties;
Expand Down Expand Up @@ -518,7 +518,7 @@ protected virtual bool SerializeMdcItem(LogEventInfo logEvent, string name, obje
object value = MappedDiagnosticsLogicalContext.GetObject(name);
if (SerializeMdlcItem(logEvent, name, value, out var serializedValue))
{
AddContextProperty(logEvent, name, value, checkForDuplicates, contextProperties);
AddContextProperty(logEvent, name, serializedValue, checkForDuplicates, contextProperties);
}
}
return contextProperties;
Expand Down
74 changes: 62 additions & 12 deletions tests/NLog.UnitTests/Targets/TargetWithContextTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,15 @@ public class TargetWithContextTest : NLogTestBase
{
public class CustomTargetWithContext : TargetWithContext
{
public bool SkipAssert { get; set; }

public class CustomTargetPropertyWithContext : TargetPropertyWithContext
{
public string Hello { get; set; }
}

[NLog.Config.ArrayParameter(typeof(CustomTargetPropertyWithContext), "contextproperty")]
public override IList<TargetPropertyWithContext> ContextProperties { get; }
public override IList<TargetPropertyWithContext> ContextProperties { get; }

public CustomTargetWithContext()
{
Expand All @@ -64,9 +66,13 @@ public CustomTargetWithContext()

protected override void Write(LogEventInfo logEvent)
{
var test = MappedDiagnosticsLogicalContext.GetNames();
Assert.Empty(test);
Assert.True(logEvent.HasStackTrace);
if (!SkipAssert)
{
var test = MappedDiagnosticsLogicalContext.GetNames();
Assert.Empty(test);
Assert.True(logEvent.HasStackTrace);
}

LastCombinedProperties = base.GetAllProperties(logEvent);
LastMessage = base.RenderLogEvent(Layout, logEvent);
}
Expand Down Expand Up @@ -102,14 +108,7 @@ public void TargetWithContextAsyncTest()
MappedDiagnosticsLogicalContext.Set("TestKey", "Hello Async World");
MappedDiagnosticsLogicalContext.Set("AsyncKey", "Hello Async World");
logger.Debug("log message");
System.Threading.Thread.Sleep(1);
for (int i = 0; i < 1000; ++i)
{
if (target.LastMessage != null)
break;

System.Threading.Thread.Sleep(1);
}
WaitForLastMessage(target);

Assert.NotEqual(0, target.LastMessage.Length);
Assert.NotNull(target.LastCombinedProperties);
Expand All @@ -124,6 +123,57 @@ public void TargetWithContextAsyncTest()
Assert.Contains(new KeyValuePair<string, object>("threadid", System.Environment.CurrentManagedThreadId.ToString()), target.LastCombinedProperties);
}

private static void WaitForLastMessage(CustomTargetWithContext target)
{
System.Threading.Thread.Sleep(1);
for (int i = 0; i < 1000; ++i)
{
if (target.LastMessage != null)
break;

System.Threading.Thread.Sleep(1);
}
}

[Fact]
public void TargetWithContextMdcSerializeTest()
{
MappedDiagnosticsContext.Clear();
MappedDiagnosticsContext.Set("TestKey", new { a = "b" });

CustomTargetWithContext target = new CustomTargetWithContext() { IncludeMdc = true, SkipAssert = true };

WriteAndAssertSingleKey(target);
}

[Fact]
public void TargetWithContextMdlcSerializeTest()
{
MappedDiagnosticsLogicalContext.Clear();
MappedDiagnosticsLogicalContext.Set("TestKey", new { a = "b" });

CustomTargetWithContext target = new CustomTargetWithContext() { IncludeMdlc = true, SkipAssert = true };

WriteAndAssertSingleKey(target);
}

private static void WriteAndAssertSingleKey(CustomTargetWithContext target)
{
AsyncTargetWrapper wrapper = new AsyncTargetWrapper { WrappedTarget = target, TimeToSleepBetweenBatches = 0 };

NLog.Config.SimpleConfigurator.ConfigureForTargetLogging(wrapper, LogLevel.Debug);

Logger logger = LogManager.GetLogger("Example");


logger.Debug("log message");

WaitForLastMessage(target);


Assert.Equal("{ a = b }", target.LastCombinedProperties["TestKey"]);
}

[Fact]
public void TargetWithContextConfigTest()
{
Expand Down

0 comments on commit 033da99

Please sign in to comment.