Skip to content

Commit 7fc50b0

Browse files
authored
Merge pull request #255 from epeshk/startswith-char
Optimization: used StartsWith(char) instead of StartsWith(string) where supported
2 parents 1e9f655 + 3dd50b2 commit 7fc50b0

File tree

3 files changed

+18
-4
lines changed

3 files changed

+18
-4
lines changed

src/Serilog.Extensions.Logging/Extensions/Logging/SerilogLogger.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,12 +103,12 @@ LogEvent PrepareWrite<TState>(LogEventLevel level, EventId eventId, TState state
103103
{
104104
messageTemplate = value;
105105
}
106-
else if (property.Key.StartsWith("@"))
106+
else if (property.Key.StartsWith('@'))
107107
{
108108
if (_logger.BindProperty(GetKeyWithoutFirstSymbol(DestructureDictionary, property.Key), property.Value, true, out var destructured))
109109
properties.Add(destructured);
110110
}
111-
else if (property.Key.StartsWith("$"))
111+
else if (property.Key.StartsWith('$'))
112112
{
113113
if (_logger.BindProperty(GetKeyWithoutFirstSymbol(StringifyDictionary, property.Key), property.Value?.ToString(), true, out var stringified))
114114
properties.Add(stringified);

src/Serilog.Extensions.Logging/Extensions/Logging/SerilogLoggerScope.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,12 @@ void AddProperty(string key, object? value)
5555
{
5656
var destructureObject = false;
5757

58-
if (key.StartsWith("@"))
58+
if (key.StartsWith('@'))
5959
{
6060
key = SerilogLogger.GetKeyWithoutFirstSymbol(SerilogLogger.DestructureDictionary, key);
6161
destructureObject = true;
6262
}
63-
else if (key.StartsWith("$"))
63+
else if (key.StartsWith('$'))
6464
{
6565
key = SerilogLogger.GetKeyWithoutFirstSymbol(SerilogLogger.StringifyDictionary, key);
6666
value = value?.ToString();
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System.Runtime.CompilerServices;
2+
3+
namespace Serilog.Extensions;
4+
5+
#if !NET6_0_OR_GREATER && !NETSTANDARD2_1_OR_GREATER
6+
static class StringExtensions
7+
{
8+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
9+
public static bool StartsWith(this string str, char value)
10+
{
11+
return str.Length > 0 && str[0] == value;
12+
}
13+
}
14+
#endif

0 commit comments

Comments
 (0)