Skip to content

Commit 3e59cae

Browse files
authored
NLogBeginScopeParser - Skip capture of nested state when List + Array + Dictionary (#662)
1 parent 8ef4b2d commit 3e59cae

File tree

3 files changed

+38
-2
lines changed

3 files changed

+38
-2
lines changed

src/NLog.Extensions.Logging/Logging/NLogBeginScopeParser.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,20 @@ public IDisposable ParseBeginScope<T>(T state)
3131
{
3232
if (state is IReadOnlyList<KeyValuePair<string, object>> scopePropertyList)
3333
{
34+
if (scopePropertyList is IList)
35+
return ScopeContext.PushNestedStateProperties(null, scopePropertyList); // Probably List/Array without nested state
36+
3437
object scopeObject = scopePropertyList;
3538
scopePropertyList = ParseScopeProperties(scopePropertyList);
3639
return ScopeContext.PushNestedStateProperties(scopeObject, scopePropertyList);
3740
}
41+
else if (state is IReadOnlyCollection<KeyValuePair<string, object>> scopeProperties)
42+
{
43+
if (scopeProperties is IDictionary)
44+
return ScopeContext.PushNestedStateProperties(null, scopeProperties); // Probably Dictionary without nested state
45+
else
46+
return ScopeContext.PushNestedStateProperties(scopeProperties, scopeProperties);
47+
}
3848

3949
if (!(state is string))
4050
{
@@ -181,7 +191,10 @@ public static IDisposable CaptureScopeProperties(IEnumerable scopePropertyCollec
181191
propertyList.Add(propertyValue.Value);
182192
}
183193

184-
return ScopeContext.PushNestedStateProperties(scopePropertyCollection, propertyList);
194+
if (scopePropertyCollection is IList || scopePropertyCollection is IDictionary)
195+
return ScopeContext.PushNestedStateProperties(null, propertyList); // Probably List/Array/Dictionary without nested state
196+
else
197+
return ScopeContext.PushNestedStateProperties(scopePropertyCollection, propertyList);
185198
}
186199

187200
public static IDisposable CaptureScopeProperty<TState>(TState scopeProperty, ExtractorDictionary stateExtractor)

test/NLog.Extensions.Logging.Tests/CustomBeginScopeTest.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,17 @@ public void TestNonSerializableSayNothing()
6565
Assert.Equal("Nothing", target.Logs[0]);
6666
}
6767

68+
[Fact]
69+
public void TestNonSerializableSaySomething()
70+
{
71+
var runner = GetRunner<CustomBeginScopeTestRunner>();
72+
var target = new Targets.MemoryTarget { Layout = "${message}${scopeproperty:Say}" };
73+
ConfigureNLog(target);
74+
runner.SaySomething().Wait();
75+
Assert.Single(target.Logs);
76+
Assert.Equal("SaySomething", target.Logs[0]);
77+
}
78+
6879
public class CustomBeginScopeTestRunner
6980
{
7081
private readonly ILogger<CustomBeginScopeTestRunner> _logger;
@@ -111,6 +122,18 @@ public async Task SayNothing()
111122
_logger.LogInformation("Nothing");
112123
}
113124
}
125+
126+
public async Task SaySomething()
127+
{
128+
using (var scopeState = _logger.BeginScope(new Dictionary<string, object>()
129+
{
130+
{ "Say", "Something" },
131+
}))
132+
{
133+
await Task.Yield();
134+
_logger.LogInformation("Say");
135+
}
136+
}
114137
}
115138

116139
private class ActionLogScope : IReadOnlyList<KeyValuePair<string, object>>

test/NLog.Extensions.Logging.Tests/Extensions/ConfigureExtensionsTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public void AddNLog_LoggerFactory_IncludeActivityIdsWithBeginScope()
7070
var logger = loggerFactory.CreateLogger(nameof(AddNLog_LoggerFactory_IncludeActivityIdsWithBeginScope));
7171
var activity = new System.Diagnostics.Activity("TestActivity").SetParentId("42").Start();
7272
var scopeProperties = new Dictionary<string, object> { { "RequestId", "123" }, { "RequestPath", "Unknown" } };
73-
using (logger.BeginScope(scopeProperties.ToList()))
73+
using (logger.BeginScope(new ArraySegment<KeyValuePair<string, object>>(scopeProperties.ToArray())))
7474
{
7575
logger.LogInformation(default(EventId), "test message with {0} arg", 1);
7676
}

0 commit comments

Comments
 (0)