Skip to content

Commit 1e9f655

Browse files
authored
Merge pull request #252 from pavel-faltynek/remove-logger-scope-side-effects
Remove logger scope side effects
2 parents 3c32997 + 8bcc366 commit 1e9f655

File tree

2 files changed

+14
-14
lines changed

2 files changed

+14
-14
lines changed

src/Serilog.Extensions.Logging/Extensions/Logging/SerilogLoggerProvider.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,7 @@ public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
7878

7979
_externalScopeProvider?.ForEachScope((state, accumulatingLogEvent) =>
8080
{
81-
var scope = new SerilogLoggerScope(this, state);
82-
83-
scope.EnrichAndCreateScopeItem(accumulatingLogEvent, propertyFactory, out var scopeItem);
81+
SerilogLoggerScope.EnrichWithStateAndCreateScopeItem(accumulatingLogEvent, propertyFactory, state, out var scopeItem);
8482

8583
if (scopeItem != null)
8684
{

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

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@ public void Dispose()
4747
}
4848
}
4949

50-
public void EnrichAndCreateScopeItem(LogEvent logEvent, ILogEventPropertyFactory propertyFactory, out LogEventPropertyValue? scopeItem)
50+
public void EnrichAndCreateScopeItem(LogEvent logEvent, ILogEventPropertyFactory propertyFactory, out LogEventPropertyValue? scopeItem) => EnrichWithStateAndCreateScopeItem(logEvent, propertyFactory, _state, out scopeItem);
51+
52+
public static void EnrichWithStateAndCreateScopeItem(LogEvent logEvent, ILogEventPropertyFactory propertyFactory, object? state, out LogEventPropertyValue? scopeItem)
5153
{
5254
void AddProperty(string key, object? value)
5355
{
@@ -68,61 +70,61 @@ void AddProperty(string key, object? value)
6870
logEvent.AddPropertyIfAbsent(property);
6971
}
7072

71-
if (_state == null)
73+
if (state == null)
7274
{
7375
scopeItem = null;
7476
return;
7577
}
7678

7779
// Eliminates boxing of Dictionary<TKey, TValue>.Enumerator for the most common use case
78-
if (_state is Dictionary<string, object> dictionary)
80+
if (state is Dictionary<string, object> dictionary)
7981
{
8082
scopeItem = null; // Unless it's `FormattedLogValues`, these are treated as property bags rather than scope items.
8183

8284
foreach (var stateProperty in dictionary)
8385
{
8486
if (stateProperty.Key == SerilogLoggerProvider.OriginalFormatPropertyName && stateProperty.Value is string)
85-
scopeItem = new ScalarValue(_state.ToString());
87+
scopeItem = new ScalarValue(state.ToString());
8688
else
8789
AddProperty(stateProperty.Key, stateProperty.Value);
8890
}
8991
}
90-
else if (_state is IEnumerable<KeyValuePair<string, object>> stateProperties)
92+
else if (state is IEnumerable<KeyValuePair<string, object>> stateProperties)
9193
{
9294
scopeItem = null; // Unless it's `FormattedLogValues`, these are treated as property bags rather than scope items.
9395

9496
foreach (var stateProperty in stateProperties)
9597
{
9698
if (stateProperty.Key == SerilogLoggerProvider.OriginalFormatPropertyName && stateProperty.Value is string)
97-
scopeItem = new ScalarValue(_state.ToString());
99+
scopeItem = new ScalarValue(state.ToString());
98100
else
99101
AddProperty(stateProperty.Key, stateProperty.Value);
100102
}
101103
}
102104
#if FEATURE_ITUPLE
103-
else if (_state is System.Runtime.CompilerServices.ITuple tuple && tuple.Length == 2 && tuple[0] is string s)
105+
else if (state is System.Runtime.CompilerServices.ITuple tuple && tuple.Length == 2 && tuple[0] is string s)
104106
{
105107
scopeItem = null; // Unless it's `FormattedLogValues`, these are treated as property bags rather than scope items.
106108

107109
if (s == SerilogLoggerProvider.OriginalFormatPropertyName && tuple[1] is string)
108-
scopeItem = new ScalarValue(_state.ToString());
110+
scopeItem = new ScalarValue(state.ToString());
109111
else
110112
AddProperty(s, tuple[1]);
111113
}
112114
#else
113-
else if (_state is ValueTuple<string, object?> tuple)
115+
else if (state is ValueTuple<string, object?> tuple)
114116
{
115117
scopeItem = null; // Unless it's `FormattedLogValues`, these are treated as property bags rather than scope items.
116118

117119
if (tuple.Item1 == SerilogLoggerProvider.OriginalFormatPropertyName && tuple.Item2 is string)
118-
scopeItem = new ScalarValue(_state.ToString());
120+
scopeItem = new ScalarValue(state.ToString());
119121
else
120122
AddProperty(tuple.Item1, tuple.Item2);
121123
}
122124
#endif
123125
else
124126
{
125-
scopeItem = propertyFactory.CreateProperty(NoName, _state).Value;
127+
scopeItem = propertyFactory.CreateProperty(NoName, state).Value;
126128
}
127129
}
128130
}

0 commit comments

Comments
 (0)