diff --git a/src/NLog.Extensions.Logging/Logging/NLogBeginScopeParser.cs b/src/NLog.Extensions.Logging/Logging/NLogBeginScopeParser.cs index f5654bca..54e9beaa 100644 --- a/src/NLog.Extensions.Logging/Logging/NLogBeginScopeParser.cs +++ b/src/NLog.Extensions.Logging/Logging/NLogBeginScopeParser.cs @@ -94,13 +94,13 @@ public static IDisposable CaptureScopeProperties(IReadOnlyList 0 && scopePropertyList[scopePropertyList.Count - 1].Key == NLogLogger.OriginalFormatPropertyName) + if (scopePropertyList.Count > 0 && NLogLogger.OriginalFormatPropertyName.Equals(scopePropertyList[scopePropertyList.Count - 1].Key)) { 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 && property.Key == NLogLogger.OriginalFormatPropertyName) + if (i == scopePropertyList.Count - 1 && i > 0 && NLogLogger.OriginalFormatPropertyName.Equals(property.Key)) { continue; // Handle BeginScope("Hello {World}", "Earth") } diff --git a/src/NLog.Extensions.Logging/Logging/NLogLogger.cs b/src/NLog.Extensions.Logging/Logging/NLogLogger.cs index 4de34719..252fbc8e 100644 --- a/src/NLog.Extensions.Logging/Logging/NLogLogger.cs +++ b/src/NLog.Extensions.Logging/Logging/NLogLogger.cs @@ -376,7 +376,7 @@ private void CaptureMessagePropertiesList(LogEventInfo eventInfo, IReadOnlyList< if (String.IsNullOrEmpty(property.Key)) continue; - if (i == messageProperties.Count - 1 && property.Key == OriginalFormatPropertyName) + if (i == messageProperties.Count - 1 && OriginalFormatPropertyName.Equals(property.Key)) continue; eventInfo.Properties[property.Key] = property.Value; diff --git a/src/NLog.Extensions.Logging/Logging/NLogMessageParameterList.cs b/src/NLog.Extensions.Logging/Logging/NLogMessageParameterList.cs index 712897c3..430e4211 100644 --- a/src/NLog.Extensions.Logging/Logging/NLogMessageParameterList.cs +++ b/src/NLog.Extensions.Logging/Logging/NLogMessageParameterList.cs @@ -11,7 +11,7 @@ namespace NLog.Extensions.Logging internal class NLogMessageParameterList : IList { private readonly IReadOnlyList> _parameterList; - private static readonly NLogMessageParameterList EmptyList = new NLogMessageParameterList(new KeyValuePair[0]); + private static readonly NLogMessageParameterList EmptyList = new NLogMessageParameterList(new KeyValuePair[0]); private static readonly NLogMessageParameterList OriginalMessageList = new NLogMessageParameterList(new[] { new KeyValuePair(NLogLogger.OriginalFormatPropertyName, string.Empty) }); public bool HasOriginalMessage => _originalMessageIndex.HasValue; @@ -44,7 +44,7 @@ public NLogMessageParameterList(IReadOnlyList> para /// public static NLogMessageParameterList TryParse(IReadOnlyList> parameterList) { - if (parameterList.Count > 1 || (parameterList.Count == 1 && parameterList[0].Key != NLogLogger.OriginalFormatPropertyName)) + if (parameterList.Count > 1 || (parameterList.Count == 1 && !NLogLogger.OriginalFormatPropertyName.Equals(parameterList[0].Key))) { return new NLogMessageParameterList(parameterList); } @@ -79,47 +79,43 @@ private static bool IsValidParameterList(IReadOnlyList= '0' && firstChar <= '9') { - hasMessageTemplateCapture = true; + if (!isPositional && i != 0) + isMixedPositional = true; + isPositional = true; } - else if (parameterKey == NLogLogger.OriginalFormatPropertyName) + else { - if (originalMessageIndex.HasValue) + if (isPositional) { - originalMessageIndex = null; - return false; + isMixedPositional = true; } - originalMessageIndex = i; - } - else - { - if (!firstParameterIsPositional.HasValue) - firstParameterIsPositional = char.IsDigit(firstChar); - else if (char.IsDigit(firstChar) != firstParameterIsPositional) - isMixedPositional = true; + if (GetCaptureType(firstChar) != CaptureType.Normal) + { + hasMessageTemplateCapture = true; + } } } - if (firstParameterIsPositional == true && !isMixedPositional) - isPositional = true; - + isPositional = isPositional && !isMixedPositional; return true; } - private static bool TryGetKey(IReadOnlyList> parameterList, ref int? originalMessageIndex, int i, out string parameterKey) + private static bool TryGetParameterName(IReadOnlyList> parameterList, int i, ref int? originalMessageIndex, out string parameterKey) { try { @@ -137,6 +133,17 @@ private static bool TryGetKey(IReadOnlyList> parame return false; } + if (NLogLogger.OriginalFormatPropertyName.Equals(parameterKey)) + { + if (originalMessageIndex.HasValue) + { + originalMessageIndex = null; + return false; + } + + originalMessageIndex = i; + } + return true; } @@ -152,7 +159,7 @@ private static IReadOnlyList> CreateValidParameterL if (string.IsNullOrEmpty(paramPair.Key)) continue; - if (paramPair.Key == NLogLogger.OriginalFormatPropertyName) + if (NLogLogger.OriginalFormatPropertyName.Equals(paramPair.Key)) { continue; } diff --git a/test/NLog.Extensions.Logging.Tests/NLogMessageParameterListTests.cs b/test/NLog.Extensions.Logging.Tests/NLogMessageParameterListTests.cs index 54396b78..feb44e9d 100644 --- a/test/NLog.Extensions.Logging.Tests/NLogMessageParameterListTests.cs +++ b/test/NLog.Extensions.Logging.Tests/NLogMessageParameterListTests.cs @@ -14,7 +14,8 @@ public void CreateNLogMessageParameterListWithEmptyKey() { new KeyValuePair("", 1), new KeyValuePair("a", 2), - new KeyValuePair("b", 3) + new KeyValuePair("b", 3), + new KeyValuePair("{OriginalFormat}", "{0}{1}{2}"), }; var list = new NLogMessageParameterList(items);