diff --git a/src/NLog.Extensions.Logging/Logging/NLogBeginScopeParser.cs b/src/NLog.Extensions.Logging/Logging/NLogBeginScopeParser.cs index 0f3bafe5..9c0feb2d 100644 --- a/src/NLog.Extensions.Logging/Logging/NLogBeginScopeParser.cs +++ b/src/NLog.Extensions.Logging/Logging/NLogBeginScopeParser.cs @@ -187,7 +187,7 @@ public static IDisposable CaptureScopeProperties(IEnumerable scopePropertyCollec continue; } - propertyList = propertyList ?? new List>(); + propertyList = propertyList ?? new List>((scopePropertyCollection as ICollection)?.Count ?? 0); propertyList.Add(propertyValue.Value); } @@ -276,7 +276,8 @@ private static bool TryBuildExtractor(Type propertyType, out KeyValuePair>(propertyKeyAccessObj, keyValuePairObjParam).Compile(); var propertyValueAccess = Expression.Property(keyValuePairTypeParam, valuePropertyInfo); - var propertyValueLambda = Expression.Lambda>(propertyValueAccess, keyValuePairObjParam).Compile(); + var propertyValueAccessObj = Expression.Convert(propertyValueAccess, typeof(object)); + var propertyValueLambda = Expression.Lambda>(propertyValueAccessObj, keyValuePairObjParam).Compile(); keyValueExtractor = new KeyValuePair, Func>(propertyKeyLambda, propertyValueLambda); return true; diff --git a/test/NLog.Extensions.Logging.Tests/LoggerTests.cs b/test/NLog.Extensions.Logging.Tests/LoggerTests.cs index 8e578d97..cd27b2eb 100644 --- a/test/NLog.Extensions.Logging.Tests/LoggerTests.cs +++ b/test/NLog.Extensions.Logging.Tests/LoggerTests.cs @@ -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()); @@ -303,7 +330,7 @@ public void LogDebugWithMessagePropertiesList() _logger.Log(Microsoft.Extensions.Logging.LogLevel.Debug, default(EventId), new List>(new[] { new KeyValuePair("ParameterCount", "1") }), null, (s, ex) => "message with id and 1 property"); } - public void LogWithScope() + public void LogWithScopeParameter() { using (_logger.BeginScope("scope1")) { @@ -311,7 +338,7 @@ public void LogWithScope() } } - public void LogWithScopeParameter() + public void LogWithScopeProperty() { using (_logger.BeginScope(new KeyValuePair("scope1", "Hello"))) { @@ -319,7 +346,15 @@ public void LogWithScopeParameter() } } - public void LogWithScopeParameterList() + public void LogWithScopeIntProperty() + { + using (_logger.BeginScope(new KeyValuePair("scope1", 42))) + { + _logger.LogDebug("message with id and {0} parameters", 1); + } + } + + public void LogWithScopePropertyList() { using (_logger.BeginScope(new[] { new KeyValuePair("scope1", "Hello") })) { @@ -327,7 +362,15 @@ public void LogWithScopeParameterList() } } - public void LogWithScopeParameterDictionary() + public void LogWithScopeIntPropertyList() + { + using (_logger.BeginScope(new[] { new KeyValuePair("scope1", 42) })) + { + _logger.LogDebug("message with id and {0} parameters", 1); + } + } + + public void LogWithScopePropertyDictionary() { using (_logger.BeginScope(new Dictionary { ["scope1"] = "Hello" })) {