Skip to content

Eliminates boxing of Dictionary<TKey, TValue>.Enumerator for the most common use case #212

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 1 commit into from
Mar 3, 2023
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 @@ -30,7 +30,7 @@ public SerilogLoggerScope(SerilogLoggerProvider provider, object state, IDisposa
}

public SerilogLoggerScope Parent { get; }

public void Dispose()
{
if (!_disposed)
Expand All @@ -51,42 +51,57 @@ public void Dispose()

public void EnrichAndCreateScopeItem(LogEvent logEvent, ILogEventPropertyFactory propertyFactory, out LogEventPropertyValue scopeItem)
{
void AddProperty(KeyValuePair<string, object> stateProperty)
{
var key = stateProperty.Key;
var destructureObject = false;
var value = stateProperty.Value;

if (key.StartsWith("@"))
{
key = key.Substring(1);
destructureObject = true;
}

if (key.StartsWith("$"))
{
key = key.Substring(1);
value = value?.ToString();
}

var property = propertyFactory.CreateProperty(key, value, destructureObject);
logEvent.AddPropertyIfAbsent(property);
}

if (_state == null)
{
scopeItem = null;
return;
}

if (_state is IEnumerable<KeyValuePair<string, object>> stateProperties)
// Eliminates boxing of Dictionary<TKey, TValue>.Enumerator for the most common use case
if (_state is Dictionary<string, object> dictionary)
{
scopeItem = null; // Unless it's `FormattedLogValues`, these are treated as property bags rather than scope items.

foreach (var stateProperty in dictionary)
{
if (stateProperty.Key == SerilogLoggerProvider.OriginalFormatPropertyName && stateProperty.Value is string)
scopeItem = new ScalarValue(_state.ToString());
else
AddProperty(stateProperty);
}
}
else if (_state is IEnumerable<KeyValuePair<string, object>> stateProperties)
{
scopeItem = null; // Unless it's `FormattedLogValues`, these are treated as property bags rather than scope items.

foreach (var stateProperty in stateProperties)
{
if (stateProperty.Key == SerilogLoggerProvider.OriginalFormatPropertyName && stateProperty.Value is string)
{
scopeItem = new ScalarValue(_state.ToString());
continue;
}

var key = stateProperty.Key;
var destructureObject = false;
var value = stateProperty.Value;

if (key.StartsWith("@"))
{
key = key.Substring(1);
destructureObject = true;
}

if (key.StartsWith("$"))
{
key = key.Substring(1);
value = value?.ToString();
}

var property = propertyFactory.CreateProperty(key, value, destructureObject);
logEvent.AddPropertyIfAbsent(property);
else
AddProperty(stateProperty);
}
}
else
Expand Down