Skip to content

Commit 8ba8d1c

Browse files
committed
moved code with preprocessor directives to extension methods
1 parent 3c6ca69 commit 8ba8d1c

File tree

3 files changed

+30
-20
lines changed

3 files changed

+30
-20
lines changed

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

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -103,20 +103,12 @@ LogEvent PrepareWrite<TState>(LogEventLevel level, EventId eventId, TState state
103103
{
104104
messageTemplate = value;
105105
}
106-
#if NET6_0_OR_GREATER || NETSTANDARD2_1_OR_GREATER
107-
else if (property.Key.StartsWith('@'))
108-
#else
109-
else if (property.Key.StartsWith("@"))
110-
#endif
106+
else if (property.Key.StartsWithAtSign())
111107
{
112108
if (_logger.BindProperty(GetKeyWithoutFirstSymbol(DestructureDictionary, property.Key), property.Value, true, out var destructured))
113109
properties.Add(destructured);
114110
}
115-
#if NET6_0_OR_GREATER || NETSTANDARD2_1_OR_GREATER
116-
else if (property.Key.StartsWith('$'))
117-
#else
118-
else if (property.Key.StartsWith("$"))
119-
#endif
111+
else if (property.Key.StartsWithDollarSign())
120112
{
121113
if (_logger.BindProperty(GetKeyWithoutFirstSymbol(StringifyDictionary, property.Key), property.Value?.ToString(), true, out var stringified))
122114
properties.Add(stringified);

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

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -54,20 +54,12 @@ public static void EnrichWithStateAndCreateScopeItem(LogEvent logEvent, ILogEven
5454
void AddProperty(string key, object? value)
5555
{
5656
var destructureObject = false;
57-
#if NET6_0_OR_GREATER || NETSTANDARD2_1_OR_GREATER
58-
if (key.StartsWith('@'))
59-
#else
60-
if (key.StartsWith("@"))
61-
#endif
57+
if (key.StartsWithAtSign())
6258
{
6359
key = SerilogLogger.GetKeyWithoutFirstSymbol(SerilogLogger.DestructureDictionary, key);
6460
destructureObject = true;
6561
}
66-
#if NET6_0_OR_GREATER || NETSTANDARD2_1_OR_GREATER
67-
else if (key.StartsWith('$'))
68-
#else
69-
else if (key.StartsWith("$"))
70-
#endif
62+
else if (key.StartsWithDollarSign())
7163
{
7264
key = SerilogLogger.GetKeyWithoutFirstSymbol(SerilogLogger.StringifyDictionary, key);
7365
value = value?.ToString();
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using System.Runtime.CompilerServices;
2+
3+
namespace Serilog.Extensions;
4+
5+
static class StringExtensions
6+
{
7+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
8+
public static bool StartsWithDollarSign(this string value)
9+
{
10+
#if NET6_0_OR_GREATER || NETSTANDARD2_1_OR_GREATER
11+
return value.StartsWith('$');
12+
#else
13+
return value.StartsWith("$");
14+
#endif
15+
}
16+
17+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
18+
public static bool StartsWithAtSign(this string value)
19+
{
20+
#if NET6_0_OR_GREATER || NETSTANDARD2_1_OR_GREATER
21+
return value.StartsWith('@');
22+
#else
23+
return value.StartsWith("@");
24+
#endif
25+
}
26+
}

0 commit comments

Comments
 (0)