From 437108ddd78d2ae19de68a198d43ea1f369889ca Mon Sep 17 00:00:00 2001 From: Rolf Kristensen Date: Sun, 30 Nov 2025 13:46:20 +0100 Subject: [PATCH 1/2] FormattedLogValues - Small optimization for index operator --- .../src/FormattedLogValues.cs | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/libraries/Microsoft.Extensions.Logging.Abstractions/src/FormattedLogValues.cs b/src/libraries/Microsoft.Extensions.Logging.Abstractions/src/FormattedLogValues.cs index e4e5a128e6a27d..9139c943b08a6f 100644 --- a/src/libraries/Microsoft.Extensions.Logging.Abstractions/src/FormattedLogValues.cs +++ b/src/libraries/Microsoft.Extensions.Logging.Abstractions/src/FormattedLogValues.cs @@ -64,17 +64,21 @@ public FormattedLogValues(string? format, params object?[]? values) { get { - if (index < 0 || index >= Count) + if (_formatter is null || _values is null) { - throw new IndexOutOfRangeException(); + if (index == 0) + { + return new KeyValuePair("{OriginalFormat}", _originalMessage); + } + else + { + throw new IndexOutOfRangeException(); + } } - - if (index == Count - 1) + else { - return new KeyValuePair("{OriginalFormat}", _originalMessage); + return _formatter.GetValue(_values, index); } - - return _formatter!.GetValue(_values!, index); } } From b83ddbe5878fdee72a79fae759edf0c29218aad3 Mon Sep 17 00:00:00 2001 From: Rolf Kristensen Date: Sun, 30 Nov 2025 14:01:04 +0100 Subject: [PATCH 2/2] Apply CoPilot suggestion of simplifying code --- .../src/FormattedLogValues.cs | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/src/libraries/Microsoft.Extensions.Logging.Abstractions/src/FormattedLogValues.cs b/src/libraries/Microsoft.Extensions.Logging.Abstractions/src/FormattedLogValues.cs index 9139c943b08a6f..dd9d053a675710 100644 --- a/src/libraries/Microsoft.Extensions.Logging.Abstractions/src/FormattedLogValues.cs +++ b/src/libraries/Microsoft.Extensions.Logging.Abstractions/src/FormattedLogValues.cs @@ -70,15 +70,11 @@ public FormattedLogValues(string? format, params object?[]? values) { return new KeyValuePair("{OriginalFormat}", _originalMessage); } - else - { - throw new IndexOutOfRangeException(); - } - } - else - { - return _formatter.GetValue(_values, index); + + throw new IndexOutOfRangeException(); } + + return _formatter.GetValue(_values, index); } }