using System; using System.Collections.Generic; using System.Linq; using ClassNotify.Shared.Models; namespace ClassNotify.Service.Diagnostics; /// /// 终端结构化诊断事件记录器接口 (P2.5 RC1-3 Support Bundle Phase 2) /// 严格限定于白名单结构化事件,不记录明文异常堆栈与通知正文 /// public interface ISupportEventRecorder { void Record(SupportDiagnosticEvent diagnosticEvent); IReadOnlyList GetRecent(DateTimeOffset sinceUtc, int maxEvents); } /// /// 基于内存有界环形缓冲区的诊断事件记录器 (最大 2000 条,最近 24 小时) /// public sealed class SupportEventRecorder : ISupportEventRecorder { private readonly object _lock = new(); private readonly LinkedList _events = new(); private const int MaxEventsCapacity = 2000; public void Record(SupportDiagnosticEvent diagnosticEvent) { if (diagnosticEvent == null) return; lock (_lock) { if (_events.Count >= MaxEventsCapacity) { _events.RemoveFirst(); } _events.AddLast(diagnosticEvent); } } public IReadOnlyList GetRecent(DateTimeOffset sinceUtc, int maxEvents) { lock (_lock) { return _events .Where(e => e.TimestampUtc >= sinceUtc) .OrderByDescending(e => e.TimestampUtc) .Take(Math.Min(maxEvents, MaxEventsCapacity)) .OrderBy(e => e.TimestampUtc) .ToList(); } } }