Skip to content

Commit 32f5e17

Browse files
committed
Optimization: used StartsWith(char) instead of StartsWith(string) where supported
1 parent 1e9f655 commit 32f5e17

File tree

3 files changed

+18
-5
lines changed

3 files changed

+18
-5
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 & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,12 @@ public static void EnrichWithStateAndCreateScopeItem(LogEvent logEvent, ILogEven
5454
void AddProperty(string key, object? value)
5555
{
5656
var destructureObject = false;
57-
58-
if (key.StartsWith("@"))
57+
if (key.StartsWith('@'))
5958
{
6059
key = SerilogLogger.GetKeyWithoutFirstSymbol(SerilogLogger.DestructureDictionary, key);
6160
destructureObject = true;
6261
}
63-
else if (key.StartsWith("$"))
62+
else if (key.StartsWith('$'))
6463
{
6564
key = SerilogLogger.GetKeyWithoutFirstSymbol(SerilogLogger.StringifyDictionary, key);
6665
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)