Skip to content

Commit 7db2eef

Browse files
authored
NLogBeginScopeParser - Improve parsing of custom KeyValuePair as scope-properties (#583)
1 parent 89c4865 commit 7db2eef

2 files changed

Lines changed: 53 additions & 9 deletions

File tree

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ public static IDisposable CaptureScopeProperties(IEnumerable scopePropertyCollec
187187
continue;
188188
}
189189

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

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

278278
var propertyValueAccess = Expression.Property(keyValuePairTypeParam, valuePropertyInfo);
279-
var propertyValueLambda = Expression.Lambda<Func<object, object>>(propertyValueAccess, keyValuePairObjParam).Compile();
279+
var propertyValueAccessObj = Expression.Convert(propertyValueAccess, typeof(object));
280+
var propertyValueLambda = Expression.Lambda<Func<object, object>>(propertyValueAccessObj, keyValuePairObjParam).Compile();
280281

281282
keyValueExtractor = new KeyValuePair<Func<object, object>, Func<object, object>>(propertyKeyLambda, propertyValueLambda);
282283
return true;

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

Lines changed: 50 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -88,27 +88,54 @@ public void TestMessagePropertiesList()
8888
}
8989

9090
[Fact]
91-
public void TestScopeProperty()
91+
public void TestScopeParameter()
9292
{
9393
GetRunner().LogWithScopeParameter();
9494

95+
var target = GetTarget();
96+
Assert.Equal("NLog.Extensions.Logging.Tests.LoggerTests.Runner|DEBUG|message with id and 1 parameters |20", target.Logs.LastOrDefault());
97+
}
98+
99+
[Fact]
100+
public void TestScopeProperty()
101+
{
102+
GetRunner().LogWithScopeProperty();
103+
95104
var target = GetTarget();
96105
Assert.Equal("NLog.Extensions.Logging.Tests.LoggerTests.Runner|DEBUG|message with id and 1 parameters |Hello", target.Logs.LastOrDefault());
97106
}
98107

108+
[Fact]
109+
public void TestScopePropertyInt()
110+
{
111+
GetRunner().LogWithScopeIntProperty();
112+
113+
var target = GetTarget();
114+
Assert.Equal("NLog.Extensions.Logging.Tests.LoggerTests.Runner|DEBUG|message with id and 1 parameters |42", target.Logs.LastOrDefault());
115+
}
116+
99117
[Fact]
100118
public void TestScopePropertyList()
101119
{
102-
GetRunner().LogWithScopeParameterList();
120+
GetRunner().LogWithScopePropertyList();
103121

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

126+
[Fact]
127+
public void TestScopeIntPropertyList()
128+
{
129+
GetRunner().LogWithScopeIntPropertyList();
130+
131+
var target = GetTarget();
132+
Assert.Equal("NLog.Extensions.Logging.Tests.LoggerTests.Runner|DEBUG|message with id and 1 parameters |42", target.Logs.LastOrDefault());
133+
}
134+
108135
[Fact]
109136
public void TestScopePropertyDictionary()
110137
{
111-
GetRunner().LogWithScopeParameterDictionary();
138+
GetRunner().LogWithScopePropertyDictionary();
112139

113140
var target = GetTarget();
114141
Assert.Equal("NLog.Extensions.Logging.Tests.LoggerTests.Runner|DEBUG|message with id and 1 parameters |Hello", target.Logs.LastOrDefault());
@@ -303,31 +330,47 @@ public void LogDebugWithMessagePropertiesList()
303330
_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");
304331
}
305332

306-
public void LogWithScope()
333+
public void LogWithScopeParameter()
307334
{
308335
using (_logger.BeginScope("scope1"))
309336
{
310337
_logger.LogDebug(20, "message with id and {0} parameters", 1);
311338
}
312339
}
313340

314-
public void LogWithScopeParameter()
341+
public void LogWithScopeProperty()
315342
{
316343
using (_logger.BeginScope(new KeyValuePair<string, string>("scope1", "Hello")))
317344
{
318345
_logger.LogDebug("message with id and {0} parameters", 1);
319346
}
320347
}
321348

322-
public void LogWithScopeParameterList()
349+
public void LogWithScopeIntProperty()
350+
{
351+
using (_logger.BeginScope(new KeyValuePair<string, int>("scope1", 42)))
352+
{
353+
_logger.LogDebug("message with id and {0} parameters", 1);
354+
}
355+
}
356+
357+
public void LogWithScopePropertyList()
323358
{
324359
using (_logger.BeginScope(new[] { new KeyValuePair<string, object>("scope1", "Hello") }))
325360
{
326361
_logger.LogDebug("message with id and {0} parameters", 1);
327362
}
328363
}
329364

330-
public void LogWithScopeParameterDictionary()
365+
public void LogWithScopeIntPropertyList()
366+
{
367+
using (_logger.BeginScope(new[] { new KeyValuePair<string, int>("scope1", 42) }))
368+
{
369+
_logger.LogDebug("message with id and {0} parameters", 1);
370+
}
371+
}
372+
373+
public void LogWithScopePropertyDictionary()
331374
{
332375
using (_logger.BeginScope(new Dictionary<string, string> { ["scope1"] = "Hello" }))
333376
{

0 commit comments

Comments
 (0)