using Logger; using System; using System.Collections.Concurrent; using System.Windows.Media; namespace UIShare.GlobalVariable { /// /// 作用域日志分发器:根据显式传入的 scope 参数把日志路由到对应 /// ,实现每个台架/作用域拥有独立 LogArea。 /// 直接写入 ConcurrentQueue,不走 Progress<T> / SynchronizationContext,避免 UI 线程压力。 /// public class ScopeLogDispatcher : IProgress<(string scope, string message, string color, int depth)> { private readonly GlobalInfo _globalInfo; /// Brush 缓存,避免每次都 new BrushConverter private static readonly ConcurrentDictionary _brushCache = new(); public ScopeLogDispatcher(GlobalInfo globalInfo) { _globalInfo = globalInfo ?? throw new ArgumentNullException(nameof(globalInfo)); } public void Report((string scope, string message, string color, int depth) value) { var scope = value.scope; if (string.IsNullOrEmpty(scope)) return; if (!_globalInfo.ContextDic.TryGetValue(scope, out var context)) return; Brush brush = _brushCache.GetOrAdd(value.color, color => { try { return (Brush)new BrushConverter().ConvertFromString(color); } catch { return Brushes.Black; } }); // 直接入队到后台线程安全的 ConcurrentQueue,不走 SynchronizationContext context.LogBuffer.Enqueue((value.message, brush, value.depth)); } } }