Skip to content

Commit

Permalink
Merge pull request #255 from epeshk/startswith-char
Browse files Browse the repository at this point in the history
Optimization: used StartsWith(char) instead of StartsWith(string) where supported
  • Loading branch information
nblumhardt authored Jul 23, 2024
2 parents 1e9f655 + 3dd50b2 commit 7fc50b0
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,12 @@ LogEvent PrepareWrite<TState>(LogEventLevel level, EventId eventId, TState state
{
messageTemplate = value;
}
else if (property.Key.StartsWith("@"))
else if (property.Key.StartsWith('@'))
{
if (_logger.BindProperty(GetKeyWithoutFirstSymbol(DestructureDictionary, property.Key), property.Value, true, out var destructured))
properties.Add(destructured);
}
else if (property.Key.StartsWith("$"))
else if (property.Key.StartsWith('$'))
{
if (_logger.BindProperty(GetKeyWithoutFirstSymbol(StringifyDictionary, property.Key), property.Value?.ToString(), true, out var stringified))
properties.Add(stringified);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,12 @@ void AddProperty(string key, object? value)
{
var destructureObject = false;

if (key.StartsWith("@"))
if (key.StartsWith('@'))
{
key = SerilogLogger.GetKeyWithoutFirstSymbol(SerilogLogger.DestructureDictionary, key);
destructureObject = true;
}
else if (key.StartsWith("$"))
else if (key.StartsWith('$'))
{
key = SerilogLogger.GetKeyWithoutFirstSymbol(SerilogLogger.StringifyDictionary, key);
value = value?.ToString();
Expand Down
14 changes: 14 additions & 0 deletions src/Serilog.Extensions.Logging/Extensions/StringExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System.Runtime.CompilerServices;

namespace Serilog.Extensions;

#if !NET6_0_OR_GREATER && !NETSTANDARD2_1_OR_GREATER
static class StringExtensions
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool StartsWith(this string str, char value)
{
return str.Length > 0 && str[0] == value;
}
}
#endif

0 comments on commit 7fc50b0

Please sign in to comment.