Skip to content

Commit

Permalink
NLogMessageParameterList - Optimize check of CaptureType for index op…
Browse files Browse the repository at this point in the history
…erator (#755)
  • Loading branch information
snakefoot authored Jul 17, 2024
1 parent 0506213 commit e41019e
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 8 deletions.
2 changes: 1 addition & 1 deletion examples/NetCore2/HostingExample/HostingExample.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Hosting" Version="7.0.1" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="6.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down
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
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Hosting" Version="6.0.1" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="6.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down

0 comments on commit e41019e

Please sign in to comment.