Skip to content

Commit b936dea

Browse files
committed
fix: 变更日志字典为线程安全字典
1 parent 658c2d3 commit b936dea

File tree

1 file changed

+8
-6
lines changed
  • framework/src/Bing.Logging/Bing/Logging

1 file changed

+8
-6
lines changed

framework/src/Bing.Logging/Bing/Logging/Log.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
using Bing.Extensions;
1+
using System.Collections.Concurrent;
2+
using Bing.Collections;
3+
using Bing.Extensions;
24
using Bing.Helpers;
35
using Bing.Logging.Core;
46
using Bing.Text;
@@ -30,7 +32,7 @@ public Log(ILoggerWrapper logger, ILogContextAccessor logContextAccessor = null)
3032
{
3133
Logger = logger ?? throw new ArgumentNullException(nameof(logger));
3234
LogContext = logContextAccessor?.Context;
33-
LogProperties = new Dictionary<string, object>();
35+
LogProperties = new ConcurrentDictionary<string, object>();
3436
LogMessage = new StringBuilder();
3537
LogMessageArgs = new List<object>();
3638
CurrentDescriptor = new LogEventDescriptor();
@@ -77,7 +79,7 @@ public Log(ILoggerWrapper logger, ILogContextAccessor logContextAccessor = null)
7779
/// <summary>
7880
/// 日志内容
7981
/// </summary>
80-
protected IDictionary<string, object> LogProperties { get; set; }
82+
protected ConcurrentDictionary<string, object> LogProperties { get; set; }
8183

8284
/// <summary>
8385
/// 日志状态
@@ -130,7 +132,7 @@ public virtual ILog Property(string propertyName, string propertyValue)
130132
LogProperties[propertyName] += propertyValue;
131133
return this;
132134
}
133-
LogProperties.Add(propertyName, propertyValue);
135+
LogProperties.TryAdd(propertyName, propertyValue);
134136
return this;
135137
}
136138

@@ -267,7 +269,7 @@ protected virtual void ConvertStateToContent()
267269
{
268270
if (item.Value.SafeString().IsEmpty())
269271
continue;
270-
LogProperties.Add(item);
272+
LogProperties.TryAdd(item.Key,item.Value);
271273
}
272274
}
273275

@@ -386,7 +388,7 @@ protected virtual void Clear()
386388
LogEventId = 0;
387389
LogException = null;
388390
LogState = null;
389-
LogProperties = new Dictionary<string, object>();
391+
LogProperties = new ConcurrentDictionary<string, object>();
390392
LogMessage.Clear();
391393
LogMessageArgs.Clear();
392394
CurrentDescriptor = new LogEventDescriptor();

0 commit comments

Comments
 (0)