@@ -103,7 +103,7 @@ public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
103
103
}
104
104
}
105
105
106
- scopeCollector . ScopeItems ? . Reverse ( ) ;
106
+ scopeCollector . ReverseItems ( ) ;
107
107
108
108
_externalScopeProvider ? . ForEachScope ( static ( state , parameters ) =>
109
109
{
@@ -120,12 +120,10 @@ public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
120
120
}
121
121
} , ( ScopeCollector : scopeCollector , PropertyFactory : propertyFactory , LogEvent : logEvent ) ) ;
122
122
123
- if ( scopeCollector . ScopeItems != null )
123
+ if ( scopeCollector . Complete ( ) is { } items )
124
124
{
125
- logEvent . AddPropertyIfAbsent ( new LogEventProperty ( ScopePropertyName , new SequenceValue ( scopeCollector . ScopeItems ) ) ) ;
125
+ logEvent . AddPropertyIfAbsent ( new LogEventProperty ( ScopePropertyName , new SequenceValue ( items ) ) ) ;
126
126
}
127
-
128
- scopeCollector . Clear ( ) ;
129
127
}
130
128
131
129
/// <inheritdoc />
@@ -159,16 +157,23 @@ public ValueTask DisposeAsync()
159
157
/// <summary>
160
158
/// A wrapper around a list to allow lazy initialization when iterating through scopes.
161
159
/// </summary>
162
- private sealed class ScopeCollector
160
+ sealed class ScopeCollector
163
161
{
164
- public List < LogEventPropertyValue > ? ScopeItems { get ; private set ; }
162
+ List < LogEventPropertyValue > ? _scopeItems ;
165
163
166
164
public void AddItem ( LogEventPropertyValue scopeItem )
167
165
{
168
- ScopeItems ??= [ ] ;
169
- ScopeItems . Add ( scopeItem ) ;
166
+ _scopeItems ??= [ ] ;
167
+ _scopeItems . Add ( scopeItem ) ;
170
168
}
171
169
172
- public void Clear ( ) => this . ScopeItems = null ;
170
+ public void ReverseItems ( ) => _scopeItems ? . Reverse ( ) ;
171
+
172
+ public List < LogEventPropertyValue > ? Complete ( )
173
+ {
174
+ var scopeItems = _scopeItems ;
175
+ _scopeItems = null ;
176
+ return scopeItems ;
177
+ }
173
178
}
174
179
}
0 commit comments