From d6a874be9e8e3ad6d17869e64bd6c009c0376f9d Mon Sep 17 00:00:00 2001 From: Rolf Kristensen Date: Mon, 14 Nov 2022 20:11:56 +0100 Subject: [PATCH] NLogBeginScopeParser - CaptureScopeProperties with cached scopePropertyCount (#639) --- .../Logging/NLogBeginScopeParser.cs | 63 +++++++++---------- .../Logging/NLogLogger.cs | 1 - .../Logging/NLogMessageParameterList.cs | 15 +++-- 3 files changed, 37 insertions(+), 42 deletions(-) diff --git a/src/NLog.Extensions.Logging/Logging/NLogBeginScopeParser.cs b/src/NLog.Extensions.Logging/Logging/NLogBeginScopeParser.cs index dea8500a..db2d72a7 100644 --- a/src/NLog.Extensions.Logging/Logging/NLogBeginScopeParser.cs +++ b/src/NLog.Extensions.Logging/Logging/NLogBeginScopeParser.cs @@ -52,17 +52,43 @@ private IDisposable CaptureScopeProperties(IReadOnlyList 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>(); + } + else + { + var propertyList = new List>(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); @@ -138,35 +164,6 @@ IEnumerator IEnumerable.GetEnumerator() } #endif - private static IReadOnlyList> ExcludeOriginalFormatProperty(IReadOnlyList> scopePropertyList) - { - if (scopePropertyList.Count == 2 && !NLogLogger.OriginalFormatPropertyName.Equals(scopePropertyList[0].Key)) - { - scopePropertyList = new[] { scopePropertyList[0] }; - } - else if (scopePropertyList.Count <= 2) - { - scopePropertyList = Array.Empty>(); - } - else - { - var propertyList = new List>(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> propertyList = null; diff --git a/src/NLog.Extensions.Logging/Logging/NLogLogger.cs b/src/NLog.Extensions.Logging/Logging/NLogLogger.cs index 70ba3424..9a3bf577 100644 --- a/src/NLog.Extensions.Logging/Logging/NLogLogger.cs +++ b/src/NLog.Extensions.Logging/Logging/NLogLogger.cs @@ -164,7 +164,6 @@ private static void CaptureLogEventInfoParameters(LogEventInfo logEvent, NLogMes } private static readonly object[] SingleItemArray = { null }; - private static readonly IList EmptyParameterArray = Array.Empty(); /// /// Are all parameters positional and correctly mapped? diff --git a/src/NLog.Extensions.Logging/Logging/NLogMessageParameterList.cs b/src/NLog.Extensions.Logging/Logging/NLogMessageParameterList.cs index 127c418b..eee5ce02 100644 --- a/src/NLog.Extensions.Logging/Logging/NLogMessageParameterList.cs +++ b/src/NLog.Extensions.Logging/Logging/NLogMessageParameterList.cs @@ -157,20 +157,20 @@ private static bool TryGetParameterName(IReadOnlyList private static IReadOnlyList> CreateValidParameterList(IReadOnlyList> parameterList) { - var validParameterList = new List>(parameterList.Count); - for (int i = 0; i < parameterList.Count; ++i) + var parameterCount = parameterList.Count; + var validParameterList = new List>(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; } @@ -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;