Skip to content

Commit

Permalink
NLogBeginScopeParser - Reduce allocation for BeginScope with message …
Browse files Browse the repository at this point in the history
…template
  • Loading branch information
snakefoot committed Sep 28, 2020
1 parent 3f903f1 commit 8a0bcab
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
24 changes: 23 additions & 1 deletion src/NLog.Extensions.Logging/Logging/NLogBeginScopeParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,28 @@ public static IDisposable CaptureScopeProperties(IReadOnlyList<KeyValuePair<stri
object scopeObject = scopePropertyList;

if (scopePropertyList.Count > 0 && NLogLogger.OriginalFormatPropertyName.Equals(scopePropertyList[scopePropertyList.Count - 1].Key))
{
scopePropertyList = ExcludeOriginalFormatProperty(scopePropertyList);
}

return CreateScopeProperties(scopeObject, scopePropertyList);
}

private static IReadOnlyList<KeyValuePair<string, object>> ExcludeOriginalFormatProperty(IReadOnlyList<KeyValuePair<string, object>> scopePropertyList)
{
if (scopePropertyList.Count == 2 && !NLogLogger.OriginalFormatPropertyName.Equals(scopePropertyList[0].Key))
{
scopePropertyList = new[] { scopePropertyList[0] };
}
else if (scopePropertyList.Count <= 2)
{
#if NET451
scopePropertyList = new KeyValuePair<string, object>[0];
#else
scopePropertyList = Array.Empty<KeyValuePair<string, object>>();
#endif
}
else
{
var propertyList = new List<KeyValuePair<string, object>>(scopePropertyList.Count - 1);
for (var i = 0; i < scopePropertyList.Count; ++i)
Expand All @@ -110,7 +132,7 @@ public static IDisposable CaptureScopeProperties(IReadOnlyList<KeyValuePair<stri
scopePropertyList = propertyList;
}

return CreateScopeProperties(scopeObject, scopePropertyList);
return scopePropertyList;
}

public static IDisposable CaptureScopeProperties(IEnumerable scopePropertyCollection, ExtractorDictionary stateExtractor)
Expand Down
22 changes: 22 additions & 0 deletions test/NLog.Extensions.Logging.Tests/CustomBeginScopeTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,18 @@ public void TestNonSerializableSayHi()
// Assert.Equal("Earth People", scopeString); <-- Bug https://github.com/aspnet/Logging/issues/893
}

[Fact]
public void TestNonSerializableSayHiToEarth()
{
var runner = GetRunner<CustomBeginScopeTestRunner>();
var target = new Targets.MemoryTarget { Layout = "${message} ${mdlc:Planet}. Welcome to the ${mdlc:Galaxy}" };
ConfigureNLog(target);
var scopeString = runner.SayHiToEarth().Result;
Assert.Single(target.Logs);
Assert.Equal("Hi Earth. Welcome to the Milky Way", target.Logs[0]);
// Assert.Equal("Earth People", scopeString); <-- Bug https://github.com/aspnet/Logging/issues/893
}

[Fact]
public void TestNonSerializableSayNothing()
{
Expand Down Expand Up @@ -82,6 +94,16 @@ public async Task<string> SayHi()
}
}

public async Task<string> SayHiToEarth()
{
using (var scopeState = _logger.BeginScope("{Planet} in {Galaxy}", "Earth", "Milky Way"))
{
await Task.Yield();
_logger.LogInformation("Hi");
return scopeState.ToString();
}
}

public async Task SayNothing()
{
using (var scopeState = _logger.BeginScope(new Dictionary<string,string>()))
Expand Down

0 comments on commit 8a0bcab

Please sign in to comment.