Skip to content

Commit f845237

Browse files
committed
It's not necessary to handle {OriginalFormat} when TState is Dictionary<K,V> or (K,V).
1 parent 62b775b commit f845237

File tree

1 file changed

+12
-16
lines changed

1 file changed

+12
-16
lines changed

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

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -55,39 +55,35 @@ public void EnrichAndCreateScopeItem(LogEvent logEvent, ILogEventPropertyFactory
5555
return;
5656
}
5757

58-
// Eliminates boxing of Dictionary<TKey, TValue>.Enumerator for the most common use case
5958
if (_state is Dictionary<string, object> dictionary)
6059
{
61-
scopeItem = null; // Unless it's `FormattedLogValues`, these are treated as property bags rather than scope items.
62-
60+
// Separate handling of this case eliminates boxing of Dictionary<TKey, TValue>.Enumerator.
61+
scopeItem = null;
6362
foreach (var stateProperty in dictionary)
6463
{
65-
if (stateProperty.Key == SerilogLoggerProvider.OriginalFormatPropertyName && stateProperty.Value is string)
66-
scopeItem = new ScalarValue(_state.ToString());
67-
else
68-
AddProperty(logEvent, propertyFactory, stateProperty.Key, stateProperty.Value);
64+
AddProperty(logEvent, propertyFactory, stateProperty.Key, stateProperty.Value);
6965
}
7066
}
7167
else if (_state is IEnumerable<KeyValuePair<string, object>> stateProperties)
7268
{
73-
scopeItem = null; // Unless it's `FormattedLogValues`, these are treated as property bags rather than scope items.
74-
69+
scopeItem = null;
7570
foreach (var stateProperty in stateProperties)
7671
{
77-
if (stateProperty.Key == SerilogLoggerProvider.OriginalFormatPropertyName && stateProperty.Value is string)
72+
if (stateProperty is { Key: SerilogLoggerProvider.OriginalFormatPropertyName, Value: string })
73+
{
74+
// `_state` is most likely `FormattedLogValues` (a MEL internal type).
7875
scopeItem = new ScalarValue(_state.ToString());
76+
}
7977
else
78+
{
8079
AddProperty(logEvent, propertyFactory, stateProperty.Key, stateProperty.Value);
80+
}
8181
}
8282
}
8383
else if (_state is ValueTuple<string, object?> tuple)
8484
{
85-
scopeItem = null; // Unless it's `FormattedLogValues`, these are treated as property bags rather than scope items.
86-
87-
if (tuple.Item1 == SerilogLoggerProvider.OriginalFormatPropertyName && tuple.Item2 is string)
88-
scopeItem = new ScalarValue(_state.ToString());
89-
else
90-
AddProperty(logEvent, propertyFactory, tuple.Item1, tuple.Item2);
85+
scopeItem = null;
86+
AddProperty(logEvent, propertyFactory, tuple.Item1, tuple.Item2);
9187
}
9288
else
9389
{

0 commit comments

Comments
 (0)