Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TargetWithContext - serialize MDC and MDLC values properly #3316

Merged
merged 1 commit into from
Apr 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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