Skip to content

Commit 9c3c6f0

Browse files
authored
NLogBeginScopeParser - Reduce allocation for BeginScope with message template (#451)
1 parent 3f903f1 commit 9c3c6f0

2 files changed

Lines changed: 44 additions & 1 deletion

File tree

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

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,28 @@ public static IDisposable CaptureScopeProperties(IReadOnlyList<KeyValuePair<stri
9595
object scopeObject = scopePropertyList;
9696

9797
if (scopePropertyList.Count > 0 && NLogLogger.OriginalFormatPropertyName.Equals(scopePropertyList[scopePropertyList.Count - 1].Key))
98+
{
99+
scopePropertyList = ExcludeOriginalFormatProperty(scopePropertyList);
100+
}
101+
102+
return CreateScopeProperties(scopeObject, scopePropertyList);
103+
}
104+
105+
private static IReadOnlyList<KeyValuePair<string, object>> ExcludeOriginalFormatProperty(IReadOnlyList<KeyValuePair<string, object>> scopePropertyList)
106+
{
107+
if (scopePropertyList.Count == 2 && !NLogLogger.OriginalFormatPropertyName.Equals(scopePropertyList[0].Key))
108+
{
109+
scopePropertyList = new[] { scopePropertyList[0] };
110+
}
111+
else if (scopePropertyList.Count <= 2)
112+
{
113+
#if NET451
114+
scopePropertyList = new KeyValuePair<string, object>[0];
115+
#else
116+
scopePropertyList = Array.Empty<KeyValuePair<string, object>>();
117+
#endif
118+
}
119+
else
98120
{
99121
var propertyList = new List<KeyValuePair<string, object>>(scopePropertyList.Count - 1);
100122
for (var i = 0; i < scopePropertyList.Count; ++i)
@@ -110,7 +132,7 @@ public static IDisposable CaptureScopeProperties(IReadOnlyList<KeyValuePair<stri
110132
scopePropertyList = propertyList;
111133
}
112134

113-
return CreateScopeProperties(scopeObject, scopePropertyList);
135+
return scopePropertyList;
114136
}
115137

116138
public static IDisposable CaptureScopeProperties(IEnumerable scopePropertyCollection, ExtractorDictionary stateExtractor)

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,17 @@ public void TestNonSerializableSayHi()
4343
// Assert.Equal("Earth People", scopeString); <-- Bug https://github.com/aspnet/Logging/issues/893
4444
}
4545

46+
[Fact]
47+
public void TestNonSerializableSayHiToEarth()
48+
{
49+
var runner = GetRunner<CustomBeginScopeTestRunner>();
50+
var target = new Targets.MemoryTarget { Layout = "${message} ${mdlc:Planet}. Welcome to the ${mdlc:Galaxy}" };
51+
ConfigureNLog(target);
52+
var scopeString = runner.SayHiToEarth().Result;
53+
Assert.Single(target.Logs);
54+
Assert.Equal("Hi Earth. Welcome to the Milky Way", target.Logs[0]);
55+
}
56+
4657
[Fact]
4758
public void TestNonSerializableSayNothing()
4859
{
@@ -82,6 +93,16 @@ public async Task<string> SayHi()
8293
}
8394
}
8495

96+
public async Task<string> SayHiToEarth()
97+
{
98+
using (var scopeState = _logger.BeginScope("{Planet} in {Galaxy}", "Earth", "Milky Way"))
99+
{
100+
await Task.Yield();
101+
_logger.LogInformation("Hi");
102+
return scopeState.ToString();
103+
}
104+
}
105+
85106
public async Task SayNothing()
86107
{
87108
using (var scopeState = _logger.BeginScope(new Dictionary<string,string>()))

0 commit comments

Comments
 (0)