Skip to content

Remove closure allocation in SerilogLoggerProvider. #277

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 4, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public sealed class SerilogLoggerProvider : ILoggerProvider, ILogEventEnricher,
// May be null; if it is, Log.Logger will be lazily used
readonly ILogger? _logger;
readonly Action? _dispose;
readonly ThreadLocal<ScopeCollector> _scopeCollector = new(() => new ScopeCollector());
#if FEATURE_ASYNCDISPOSABLE
readonly Func<ValueTask>? _disposeAsync;
#endif
Expand Down Expand Up @@ -90,35 +91,38 @@ public IDisposable BeginScope<T>(T state)
/// <inheritdoc />
public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
{
List<LogEventPropertyValue>? scopeItems = null;
var scopeCollector = _scopeCollector.Value!;

for (var scope = CurrentScope; scope != null; scope = scope.Parent)
{
scope.EnrichAndCreateScopeItem(logEvent, propertyFactory, out var scopeItem);

if (scopeItem != null)
{
scopeItems ??= [];
scopeItems.Add(scopeItem);
scopeCollector.AddItem(scopeItem);
}
}

scopeItems?.Reverse();
scopeCollector.ReverseItems();

_externalScopeProvider?.ForEachScope((state, accumulatingLogEvent) =>
_externalScopeProvider?.ForEachScope(static (state, parameters) =>
{
SerilogLoggerScope.EnrichWithStateAndCreateScopeItem(
accumulatingLogEvent, propertyFactory, state, update: true, out var scopeItem);
parameters.LogEvent,
parameters.PropertyFactory,
state,
update: true,
out var scopeItem);

if (scopeItem != null)
{
scopeItems ??= new List<LogEventPropertyValue>();
scopeItems.Add(scopeItem);
parameters.ScopeCollector.AddItem(scopeItem);
}
}, logEvent);
}, (ScopeCollector: scopeCollector, PropertyFactory: propertyFactory, LogEvent: logEvent));

if (scopeItems != null)
if (scopeCollector.Complete() is { } items)
{
logEvent.AddPropertyIfAbsent(new LogEventProperty(ScopePropertyName, new SequenceValue(scopeItems)));
logEvent.AddPropertyIfAbsent(new LogEventProperty(ScopePropertyName, new SequenceValue(items)));
}
}

Expand Down Expand Up @@ -149,4 +153,27 @@ public ValueTask DisposeAsync()
return _disposeAsync?.Invoke() ?? default;
}
#endif

/// <summary>
/// A wrapper around a list to allow lazy initialization when iterating through scopes.
/// </summary>
sealed class ScopeCollector
{
List<LogEventPropertyValue>? _scopeItems;

public void AddItem(LogEventPropertyValue scopeItem)
{
_scopeItems ??= [];
_scopeItems.Add(scopeItem);
}

public void ReverseItems() => _scopeItems?.Reverse();

public List<LogEventPropertyValue>? Complete()
{
var scopeItems = _scopeItems;
_scopeItems = null;
return scopeItems;
}
}
}