Skip to content

Commit ccae6ff

Browse files
committed
Remove some accidental usages of throwing Dictionary<,>.Add()
1 parent bcc5021 commit ccae6ff

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

src/Serilog.Extensions.Logging/Extensions/Logging/SerilogLogger.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -106,20 +106,20 @@ LogEvent PrepareWrite<TState>(LogEventLevel level, EventId eventId, TState state
106106
else if (property.Key.StartsWith("@", StringComparison.Ordinal))
107107
{
108108
if (_logger.BindProperty(GetKeyWithoutFirstSymbol(DestructureDictionary, property.Key), property.Value, true, out var destructured))
109-
properties.Add(destructured.Name, destructured.Value);
109+
properties[destructured.Name] = destructured.Value;
110110
}
111111
else if (property.Key.StartsWith("$", StringComparison.Ordinal))
112112
{
113113
if (_logger.BindProperty(GetKeyWithoutFirstSymbol(StringifyDictionary, property.Key), property.Value?.ToString(), true, out var stringified))
114-
properties.Add(stringified.Name, stringified.Value);
114+
properties[stringified.Name] = stringified.Value;
115115
}
116116
else
117117
{
118118
// Simple micro-optimization for the most common and reliably scalar values; could go further here.
119119
if (property.Value is null or string or int or long && LogEventProperty.IsValidName(property.Key))
120-
properties.Add(property.Key, new ScalarValue(property.Value));
120+
properties[property.Key] = new ScalarValue(property.Value);
121121
else if (_logger.BindProperty(property.Key, property.Value, false, out var bound))
122-
properties.Add(bound.Name, bound.Value);
122+
properties[bound.Name] = bound.Value;
123123
}
124124
}
125125

@@ -130,7 +130,7 @@ LogEvent PrepareWrite<TState>(LogEventLevel level, EventId eventId, TState state
130130
{
131131
messageTemplate = "{" + stateType.Name + ":l}";
132132
if (_logger.BindProperty(stateType.Name, AsLoggableValue(state, formatter), false, out var stateTypeProperty))
133-
properties.Add(stateTypeProperty.Name, stateTypeProperty.Value);
133+
properties[stateTypeProperty.Name] = stateTypeProperty.Value;
134134
}
135135
}
136136

@@ -153,18 +153,18 @@ LogEvent PrepareWrite<TState>(LogEventLevel level, EventId eventId, TState state
153153
if (propertyName != null)
154154
{
155155
if (_logger.BindProperty(propertyName, AsLoggableValue(state, formatter!), false, out var property))
156-
properties.Add(property.Name, property.Value);
156+
properties[property.Name] = property.Value;
157157
}
158158
}
159159

160160
if (eventId != default)
161-
properties.Add("EventId", CreateEventIdPropertyValue(eventId));
161+
properties["EventId"] = CreateEventIdPropertyValue(eventId);
162162

163163
var (traceId, spanId) = Activity.Current is { } activity ?
164164
(activity.TraceId, activity.SpanId) :
165165
(default(ActivityTraceId), default(ActivitySpanId));
166166

167-
var parsedTemplate = MessageTemplateParser.Parse(messageTemplate ?? "");
167+
var parsedTemplate = messageTemplate != null ? MessageTemplateParser.Parse(messageTemplate) : MessageTemplate.Empty;
168168
return LogEvent.UnstableAssembleFromParts(DateTimeOffset.Now, level, exception, parsedTemplate, properties, traceId, spanId);
169169
}
170170

0 commit comments

Comments
 (0)