Skip to content

Commit cee6a1a

Browse files
committed
Make DiagnosticContextCollector thread-safe
1 parent 3b88ae0 commit cee6a1a

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

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

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ namespace Serilog.Extensions.Hosting
1010
public sealed class DiagnosticContextCollector : IDisposable
1111
{
1212
readonly AmbientDiagnosticContextCollector _ambientCollector;
13+
readonly object _propertiesLock = new object();
1314
List<LogEventProperty> _properties = new List<LogEventProperty>();
1415

1516
internal DiagnosticContextCollector(AmbientDiagnosticContextCollector ambientCollector)
@@ -24,7 +25,11 @@ internal DiagnosticContextCollector(AmbientDiagnosticContextCollector ambientCol
2425
public void Add(LogEventProperty property)
2526
{
2627
if (property == null) throw new ArgumentNullException(nameof(property));
27-
_properties?.Add(property);
28+
29+
lock (_propertiesLock)
30+
{
31+
_properties?.Add(property);
32+
}
2833
}
2934

3035
/// <summary>
@@ -36,10 +41,13 @@ public void Add(LogEventProperty property)
3641
/// <returns>True if properties could be collected.</returns>
3742
public bool TryComplete(out List<LogEventProperty> properties)
3843
{
39-
properties = _properties;
40-
_properties = null;
41-
Dispose();
42-
return properties != null;
44+
lock (_propertiesLock)
45+
{
46+
properties = _properties;
47+
_properties = null;
48+
Dispose();
49+
return properties != null;
50+
}
4351
}
4452

4553
/// <inheritdoc/>

0 commit comments

Comments
 (0)