Skip to content

Commit

Permalink
NLogMessageParameterList - Optimize index operator to skip paramater-…
Browse files Browse the repository at this point in the history
…name validation when no message template capture
  • Loading branch information
snakefoot committed Jul 17, 2024
1 parent 0506213 commit 64a0d59
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions src/NLog.Extensions.Logging/Logging/NLogMessageParameterList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ private static bool IsValidParameterList(IReadOnlyList<KeyValuePair<string, obje
string parameterName;

var parameterCount = parameterList.Count;

for (int i = 0; i < parameterCount; ++i)
{
if (!TryGetParameterName(parameterList, i, out parameterName))
Expand Down Expand Up @@ -188,15 +187,21 @@ public MessageTemplateParameter this[int index]
index += 1;

var parameter = _parameterList[index];
var parameterName = parameter.Key;
var capture = GetCaptureType(parameterName[0]);
if (capture != CaptureType.Normal)
parameterName = parameterName.Substring(1);
return new MessageTemplateParameter(parameterName, parameter.Value, null, capture);
return _hasMessageTemplateCapture ?
GetMessageTemplateParameter(parameter.Key, parameter.Value) :
new MessageTemplateParameter(parameter.Key, parameter.Value, null, CaptureType.Normal);
}
set => throw new NotSupportedException();
}

private static MessageTemplateParameter GetMessageTemplateParameter(string parameterName, object parameterValue)
{
var capture = GetCaptureType(parameterName[0]);
if (capture != CaptureType.Normal)
parameterName = parameterName.Substring(1);
return new MessageTemplateParameter(parameterName, parameterValue, null, capture);
}

private static CaptureType GetCaptureType(char firstChar)
{
if (firstChar == '@')
Expand Down

0 comments on commit 64a0d59

Please sign in to comment.