Skip to content

Commit

Permalink
NLogBeginScopeParser - Reduce complexity of ParseScopeProperties (#656)
Browse files Browse the repository at this point in the history
  • Loading branch information
snakefoot authored Jan 22, 2023
1 parent 60fcfa6 commit 32c9e8f
Showing 1 changed file with 6 additions and 8 deletions.
14 changes: 6 additions & 8 deletions src/NLog.Extensions.Logging/Logging/NLogBeginScopeParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,34 +54,32 @@ private IReadOnlyList<KeyValuePair<string, object>> ParseScopeProperties(IReadOn
{
var scopePropertyCount = scopePropertyList.Count;
if (scopePropertyCount == 0)
return Array.Empty<KeyValuePair<string, object>>();
return scopePropertyList;

if (!NLogLogger.OriginalFormatPropertyName.Equals(scopePropertyList[scopePropertyCount - 1].Key))
return IncludeActivityIdsProperties(scopePropertyList);
else
scopePropertyCount -= 1; // Handle BeginScope("Hello {World}", "Earth")

var firstProperty = scopePropertyList[0];
if (scopePropertyCount == 2 && !string.IsNullOrEmpty(firstProperty.Key) && !NLogLogger.OriginalFormatPropertyName.Equals(firstProperty.Key))
if (scopePropertyCount == 1 && !string.IsNullOrEmpty(firstProperty.Key))
{
return new[] { firstProperty };
}
else if (scopePropertyCount <= 2)
else if (scopePropertyCount == 0)
{
return Array.Empty<KeyValuePair<string, object>>();
}
else
{
var propertyList = new List<KeyValuePair<string, object>>(scopePropertyCount - 1);
var propertyList = new List<KeyValuePair<string, object>>(scopePropertyCount);
for (var i = 0; i < scopePropertyCount; ++i)
{
var property = scopePropertyList[i];
if (string.IsNullOrEmpty(property.Key))
{
continue;
}
if (NLogLogger.OriginalFormatPropertyName.Equals(property.Key))
{
continue; // Handle BeginScope("Hello {World}", "Earth")
}
propertyList.Add(property);
}
return propertyList;
Expand Down

0 comments on commit 32c9e8f

Please # to comment.