Skip to content

Commit 285de7a

Browse files
authored
Avoid boxing ITuple when used for BeginScope (refactor) (#816)
1 parent f615530 commit 285de7a

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

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

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,34 +29,36 @@ public IDisposable ParseBeginScope<T>(T state)
2929
{
3030
if (_options.CaptureMessageProperties)
3131
{
32-
if (state is IReadOnlyList<KeyValuePair<string, object?>> scopePropertyList)
32+
if (state is IReadOnlyList<KeyValuePair<string, object?>>)
3333
{
34+
var scopePropertyList = (IReadOnlyList<KeyValuePair<string, object?>>)state;
3435
if (scopePropertyList is IList)
3536
return ScopeContext.PushNestedStateProperties(null, scopePropertyList); // Probably List/Array without nested state
3637

3738
object scopeObject = scopePropertyList;
3839
scopePropertyList = ParseScopeProperties(scopePropertyList);
3940
return ScopeContext.PushNestedStateProperties(scopeObject, scopePropertyList);
4041
}
41-
else if (state is IReadOnlyCollection<KeyValuePair<string, object?>> scopeProperties)
42-
{
43-
if (scopeProperties is IDictionary)
44-
return ScopeContext.PushNestedStateProperties(null, scopeProperties); // Probably Dictionary without nested state
45-
else
46-
return ScopeContext.PushNestedStateProperties(scopeProperties, scopeProperties);
47-
}
4842
#if NETSTANDARD2_1_OR_GREATER || NETCOREAPP3_1_OR_GREATER || NET471_OR_GREATER
4943
else if (state is System.Runtime.CompilerServices.ITuple && ((System.Runtime.CompilerServices.ITuple)state).Length == 2 && ((System.Runtime.CompilerServices.ITuple)state)[0] is string propertyName)
5044
{
5145
return ScopeContext.PushProperty(propertyName, ((System.Runtime.CompilerServices.ITuple)state)[1]);
5246
}
5347
#endif
48+
else if (state is IReadOnlyCollection<KeyValuePair<string, object?>>)
49+
{
50+
var scopeProperties = (IReadOnlyCollection<KeyValuePair<string, object?>>)state;
51+
if (state is IDictionary)
52+
return ScopeContext.PushNestedStateProperties(null, scopeProperties); // Probably Dictionary without nested state
53+
else
54+
return ScopeContext.PushNestedStateProperties(scopeProperties, scopeProperties);
55+
}
5456

5557
if (!(state is string))
5658
{
57-
if (state is IEnumerable scopePropertyCollection)
59+
if (state is IEnumerable)
5860
{
59-
return CaptureScopeProperties(scopePropertyCollection, _scopeStateExtractors);
61+
return CaptureScopeProperties((IEnumerable)state, _scopeStateExtractors);
6062
}
6163

6264
return CaptureScopeProperty(state, _scopeStateExtractors);

0 commit comments

Comments
 (0)