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

NLogBeginScopeParser - Skip capture of nested state when List + Array + Dictionary #662

Merged
merged 1 commit into from
Mar 28, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion src/NLog.Extensions.Logging/Logging/NLogBeginScopeParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,20 @@ public IDisposable ParseBeginScope<T>(T state)
{
if (state is IReadOnlyList<KeyValuePair<string, object>> scopePropertyList)
{
if (scopePropertyList is IList)
return ScopeContext.PushNestedStateProperties(null, scopePropertyList); // Probably List/Array without nested state

object scopeObject = scopePropertyList;
scopePropertyList = ParseScopeProperties(scopePropertyList);
return ScopeContext.PushNestedStateProperties(scopeObject, scopePropertyList);
}
else if (state is IReadOnlyCollection<KeyValuePair<string, object>> scopeProperties)
{
if (scopeProperties is IDictionary)
return ScopeContext.PushNestedStateProperties(null, scopeProperties); // Probably Dictionary without nested state
else
return ScopeContext.PushNestedStateProperties(scopeProperties, scopeProperties);
}

if (!(state is string))
{
Expand Down Expand Up @@ -181,7 +191,10 @@ public static IDisposable CaptureScopeProperties(IEnumerable scopePropertyCollec
propertyList.Add(propertyValue.Value);
}

return ScopeContext.PushNestedStateProperties(scopePropertyCollection, propertyList);
if (scopePropertyCollection is IList || scopePropertyCollection is IDictionary)
return ScopeContext.PushNestedStateProperties(null, propertyList); // Probably List/Array/Dictionary without nested state
else
return ScopeContext.PushNestedStateProperties(scopePropertyCollection, propertyList);
}

public static IDisposable CaptureScopeProperty<TState>(TState scopeProperty, ExtractorDictionary stateExtractor)
Expand Down
23 changes: 23 additions & 0 deletions test/NLog.Extensions.Logging.Tests/CustomBeginScopeTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,17 @@ public void TestNonSerializableSayNothing()
Assert.Equal("Nothing", target.Logs[0]);
}

[Fact]
public void TestNonSerializableSaySomething()
{
var runner = GetRunner<CustomBeginScopeTestRunner>();
var target = new Targets.MemoryTarget { Layout = "${message}${scopeproperty:Say}" };
ConfigureNLog(target);
runner.SaySomething().Wait();
Assert.Single(target.Logs);
Assert.Equal("SaySomething", target.Logs[0]);
}

public class CustomBeginScopeTestRunner
{
private readonly ILogger<CustomBeginScopeTestRunner> _logger;
Expand Down Expand Up @@ -111,6 +122,18 @@ public async Task SayNothing()
_logger.LogInformation("Nothing");
}
}

public async Task SaySomething()
{
using (var scopeState = _logger.BeginScope(new Dictionary<string, object>()
{
{ "Say", "Something" },
}))
{
await Task.Yield();
_logger.LogInformation("Say");
}
}
}

private class ActionLogScope : IReadOnlyList<KeyValuePair<string, object>>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public void AddNLog_LoggerFactory_IncludeActivityIdsWithBeginScope()
var logger = loggerFactory.CreateLogger(nameof(AddNLog_LoggerFactory_IncludeActivityIdsWithBeginScope));
var activity = new System.Diagnostics.Activity("TestActivity").SetParentId("42").Start();
var scopeProperties = new Dictionary<string, object> { { "RequestId", "123" }, { "RequestPath", "Unknown" } };
using (logger.BeginScope(scopeProperties.ToList()))
using (logger.BeginScope(new ArraySegment<KeyValuePair<string, object>>(scopeProperties.ToArray())))
{
logger.LogInformation(default(EventId), "test message with {0} arg", 1);
}
Expand Down