Skip to content

Commit

Permalink
NLogBeginScopeParser - CaptureScopeProperties with cached scopeProper…
Browse files Browse the repository at this point in the history
…tyCount (#639)
  • Loading branch information
snakefoot authored Nov 14, 2022
1 parent 9442b97 commit d6a874b
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 42 deletions.
63 changes: 30 additions & 33 deletions src/NLog.Extensions.Logging/Logging/NLogBeginScopeParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,43 @@ private IDisposable CaptureScopeProperties(IReadOnlyList<KeyValuePair<string, ob
{
object scopeObject = scopePropertyList;

if (scopePropertyList.Count > 0)
var scopePropertyCount = scopePropertyList.Count;
if (scopePropertyCount > 0)
{
if (NLogLogger.OriginalFormatPropertyName.Equals(scopePropertyList[scopePropertyList.Count - 1].Key))
if (NLogLogger.OriginalFormatPropertyName.Equals(scopePropertyList[scopePropertyCount - 1].Key))
{
scopePropertyList = ExcludeOriginalFormatProperty(scopePropertyList);
var firstProperty = scopePropertyList[0];
if (scopePropertyCount == 2 && !string.IsNullOrEmpty(firstProperty.Key) && !NLogLogger.OriginalFormatPropertyName.Equals(firstProperty.Key))
{
scopePropertyList = new[] { firstProperty };
}
else if (scopePropertyCount <= 2)
{
scopePropertyList = Array.Empty<KeyValuePair<string, object>>();
}
else
{
var propertyList = new List<KeyValuePair<string, object>>(scopePropertyCount - 1);
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);
}
scopePropertyList = propertyList;
}
}
else
{
scopePropertyList = IncludeActivityIdsProperties(scopePropertyList);
}

}

return ScopeContext.PushNestedStateProperties(scopeObject, scopePropertyList);
Expand Down Expand Up @@ -138,35 +164,6 @@ IEnumerator IEnumerable.GetEnumerator()
}
#endif

private static IReadOnlyList<KeyValuePair<string, object>> ExcludeOriginalFormatProperty(IReadOnlyList<KeyValuePair<string, object>> scopePropertyList)
{
if (scopePropertyList.Count == 2 && !NLogLogger.OriginalFormatPropertyName.Equals(scopePropertyList[0].Key))
{
scopePropertyList = new[] { scopePropertyList[0] };
}
else if (scopePropertyList.Count <= 2)
{
scopePropertyList = Array.Empty<KeyValuePair<string, object>>();
}
else
{
var propertyList = new List<KeyValuePair<string, object>>(scopePropertyList.Count - 1);
for (var i = 0; i < scopePropertyList.Count; ++i)
{
var property = scopePropertyList[i];
if (i == scopePropertyList.Count - 1 && i > 0 && NLogLogger.OriginalFormatPropertyName.Equals(property.Key))
{
continue; // Handle BeginScope("Hello {World}", "Earth")
}

propertyList.Add(property);
}
scopePropertyList = propertyList;
}

return scopePropertyList;
}

public static IDisposable CaptureScopeProperties(IEnumerable scopePropertyCollection, ExtractorDictionary stateExtractor)
{
List<KeyValuePair<string, object>> propertyList = null;
Expand Down
1 change: 0 additions & 1 deletion src/NLog.Extensions.Logging/Logging/NLogLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,6 @@ private static void CaptureLogEventInfoParameters(LogEventInfo logEvent, NLogMes
}

private static readonly object[] SingleItemArray = { null };
private static readonly IList<MessageTemplateParameter> EmptyParameterArray = Array.Empty<MessageTemplateParameter>();

/// <summary>
/// Are all parameters positional and correctly mapped?
Expand Down
15 changes: 7 additions & 8 deletions src/NLog.Extensions.Logging/Logging/NLogMessageParameterList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -157,20 +157,20 @@ private static bool TryGetParameterName(IReadOnlyList<KeyValuePair<string, objec
/// </summary>
private static IReadOnlyList<KeyValuePair<string, object>> CreateValidParameterList(IReadOnlyList<KeyValuePair<string, object>> parameterList)
{
var validParameterList = new List<KeyValuePair<string, object>>(parameterList.Count);
for (int i = 0; i < parameterList.Count; ++i)
var parameterCount = parameterList.Count;
var validParameterList = new List<KeyValuePair<string, object>>(parameterCount);

for (int i = 0; i < parameterCount; ++i)
{
var paramPair = parameterList[i];
if (string.IsNullOrEmpty(paramPair.Key))
if (!TryGetParameterName(parameterList, i, out var parameterName))
continue;

if (NLogLogger.OriginalFormatPropertyName.Equals(paramPair.Key))
{
if (NLogLogger.OriginalFormatPropertyName.Equals(parameterName))
continue;
}

validParameterList.Add(parameterList[i]);
}

return validParameterList;
}

Expand Down Expand Up @@ -201,7 +201,6 @@ private static CaptureType GetCaptureType(char firstChar)
return CaptureType.Normal;
}


public int Count => _parameterList.Count - (_originalMessageIndex.HasValue ? 1 : 0);

public bool IsReadOnly => true;
Expand Down

0 comments on commit d6a874b

Please sign in to comment.