Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NLogBeginScopeParser - Improve parsing of custom KeyValuePair as scope-properties #583

Merged
merged 1 commit into from
Apr 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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