Skip to content

Commit df57744

Browse files
committed
Probably not paranoid here to trade some actual performance for better worst-case execution time
1 parent 3a5f079 commit df57744

File tree

1 file changed

+4
-14
lines changed

1 file changed

+4
-14
lines changed

src/Serilog.Extensions.Hosting/Extensions/Hosting/DiagnosticContextCollector.cs

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public sealed class DiagnosticContextCollector : IDisposable
1111
{
1212
readonly IDisposable _chainedDisposable;
1313
readonly object _propertiesLock = new object();
14-
List<LogEventProperty> _properties = new List<LogEventProperty>();
14+
Dictionary<string, LogEventProperty> _properties = new Dictionary<string, LogEventProperty>();
1515

1616
/// <summary>
1717
/// Construct a <see cref="DiagnosticContextCollector"/>.
@@ -34,17 +34,7 @@ public void AddOrUpdate(LogEventProperty property)
3434
lock (_propertiesLock)
3535
{
3636
if (_properties == null) return;
37-
38-
for (var i = 0; i < _properties.Count; ++i)
39-
{
40-
if (_properties[i].Name == property.Name)
41-
{
42-
_properties[i] = property;
43-
return;
44-
}
45-
}
46-
47-
_properties.Add(property);
37+
_properties[property.Name] = property;
4838
}
4939
}
5040

@@ -55,11 +45,11 @@ public void AddOrUpdate(LogEventProperty property)
5545
/// </summary>
5646
/// <param name="properties">The collected properties, or null if no collection is active.</param>
5747
/// <returns>True if properties could be collected.</returns>
58-
public bool TryComplete(out List<LogEventProperty> properties)
48+
public bool TryComplete(out IEnumerable<LogEventProperty> properties)
5949
{
6050
lock (_propertiesLock)
6151
{
62-
properties = _properties;
52+
properties = _properties?.Values;
6353
_properties = null;
6454
Dispose();
6555
return properties != null;

0 commit comments

Comments
 (0)