Skip to content

Commit

Permalink
Merge pull request #212 from sungam3r/enumerator
Browse files Browse the repository at this point in the history
Eliminates boxing of Dictionary<TKey, TValue>.Enumerator for the most common use case
  • Loading branch information
nblumhardt authored Mar 3, 2023
2 parents 5bd2cc0 + 2e5bec9 commit 6539e2d
Showing 1 changed file with 39 additions and 24 deletions.
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

0 comments on commit 6539e2d

Please sign in to comment.