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 - CaptureScopeProperties with cached scopePropertyCount #639

Merged
merged 1 commit into from
Nov 14, 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
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