Skip to content

Commit

Permalink
NLogBeginScopeParser - Improve parsing of custom KeyValuePair as scop…
Browse files Browse the repository at this point in the history
…e-properties (#583)
  • Loading branch information
snakefoot authored Apr 5, 2022
1 parent 89c4865 commit 7db2eef
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 9 deletions.
5 changes: 3 additions & 2 deletions src/NLog.Extensions.Logging/Logging/NLogBeginScopeParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ public static IDisposable CaptureScopeProperties(IEnumerable scopePropertyCollec
continue;
}

propertyList = propertyList ?? new List<KeyValuePair<string, object>>();
propertyList = propertyList ?? new List<KeyValuePair<string, object>>((scopePropertyCollection as ICollection)?.Count ?? 0);
propertyList.Add(propertyValue.Value);
}

Expand Down Expand Up @@ -276,7 +276,8 @@ private static bool TryBuildExtractor(Type propertyType, out KeyValuePair<Func<o
var propertyKeyLambda = Expression.Lambda<Func<object, object>>(propertyKeyAccessObj, keyValuePairObjParam).Compile();

var propertyValueAccess = Expression.Property(keyValuePairTypeParam, valuePropertyInfo);
var propertyValueLambda = Expression.Lambda<Func<object, object>>(propertyValueAccess, keyValuePairObjParam).Compile();
var propertyValueAccessObj = Expression.Convert(propertyValueAccess, typeof(object));
var propertyValueLambda = Expression.Lambda<Func<object, object>>(propertyValueAccessObj, keyValuePairObjParam).Compile();

keyValueExtractor = new KeyValuePair<Func<object, object>, Func<object, object>>(propertyKeyLambda, propertyValueLambda);
return true;
Expand Down
57 changes: 50 additions & 7 deletions test/NLog.Extensions.Logging.Tests/LoggerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,27 +88,54 @@ public void TestMessagePropertiesList()
}

[Fact]
public void TestScopeProperty()
public void TestScopeParameter()
{
GetRunner().LogWithScopeParameter();

var target = GetTarget();
Assert.Equal("NLog.Extensions.Logging.Tests.LoggerTests.Runner|DEBUG|message with id and 1 parameters |20", target.Logs.LastOrDefault());
}

[Fact]
public void TestScopeProperty()
{
GetRunner().LogWithScopeProperty();

var target = GetTarget();
Assert.Equal("NLog.Extensions.Logging.Tests.LoggerTests.Runner|DEBUG|message with id and 1 parameters |Hello", target.Logs.LastOrDefault());
}

[Fact]
public void TestScopePropertyInt()
{
GetRunner().LogWithScopeIntProperty();

var target = GetTarget();
Assert.Equal("NLog.Extensions.Logging.Tests.LoggerTests.Runner|DEBUG|message with id and 1 parameters |42", target.Logs.LastOrDefault());
}

[Fact]
public void TestScopePropertyList()
{
GetRunner().LogWithScopeParameterList();
GetRunner().LogWithScopePropertyList();

var target = GetTarget();
Assert.Equal("NLog.Extensions.Logging.Tests.LoggerTests.Runner|DEBUG|message with id and 1 parameters |Hello", target.Logs.LastOrDefault());
}

[Fact]
public void TestScopeIntPropertyList()
{
GetRunner().LogWithScopeIntPropertyList();

var target = GetTarget();
Assert.Equal("NLog.Extensions.Logging.Tests.LoggerTests.Runner|DEBUG|message with id and 1 parameters |42", target.Logs.LastOrDefault());
}

[Fact]
public void TestScopePropertyDictionary()
{
GetRunner().LogWithScopeParameterDictionary();
GetRunner().LogWithScopePropertyDictionary();

var target = GetTarget();
Assert.Equal("NLog.Extensions.Logging.Tests.LoggerTests.Runner|DEBUG|message with id and 1 parameters |Hello", target.Logs.LastOrDefault());
Expand Down Expand Up @@ -303,31 +330,47 @@ public void LogDebugWithMessagePropertiesList()
_logger.Log(Microsoft.Extensions.Logging.LogLevel.Debug, default(EventId), new List<KeyValuePair<string, object>>(new[] { new KeyValuePair<string, object>("ParameterCount", "1") }), null, (s, ex) => "message with id and 1 property");
}

public void LogWithScope()
public void LogWithScopeParameter()
{
using (_logger.BeginScope("scope1"))
{
_logger.LogDebug(20, "message with id and {0} parameters", 1);
}
}

public void LogWithScopeParameter()
public void LogWithScopeProperty()
{
using (_logger.BeginScope(new KeyValuePair<string, string>("scope1", "Hello")))
{
_logger.LogDebug("message with id and {0} parameters", 1);
}
}

public void LogWithScopeParameterList()
public void LogWithScopeIntProperty()
{
using (_logger.BeginScope(new KeyValuePair<string, int>("scope1", 42)))
{
_logger.LogDebug("message with id and {0} parameters", 1);
}
}

public void LogWithScopePropertyList()
{
using (_logger.BeginScope(new[] { new KeyValuePair<string, object>("scope1", "Hello") }))
{
_logger.LogDebug("message with id and {0} parameters", 1);
}
}

public void LogWithScopeParameterDictionary()
public void LogWithScopeIntPropertyList()
{
using (_logger.BeginScope(new[] { new KeyValuePair<string, int>("scope1", 42) }))
{
_logger.LogDebug("message with id and {0} parameters", 1);
}
}

public void LogWithScopePropertyDictionary()
{
using (_logger.BeginScope(new Dictionary<string, string> { ["scope1"] = "Hello" }))
{
Expand Down

0 comments on commit 7db2eef

Please sign in to comment.