// Copyright (c) 2025-2026 GeWuYou // SPDX-License-Identifier: Apache-2.0 using System; using System.Collections.Generic; using System.Threading; using GFramework.Core.Abstractions.Logging; using GFramework.Core.Logging; namespace GFramework.Core.Tests.Logging; /// /// 表示供日志相关测试复用的内存日志记录器。 /// /// /// 并发写入会通过内部锁串行化; 每次返回快照,避免断言观察到正在被修改的可变集合。 /// public sealed class TestLogger : AbstractLogger { private readonly List _logs = new(); private readonly Lock _sync = new(); /// /// 初始化 的新实例。 /// /// 日志记录器的名称;未指定时沿用基类默认行为。 /// 允许写入的最小日志级别。 public TestLogger(string? name = null, LogLevel minLevel = LogLevel.Info) : base(name, minLevel) { } /// /// 获取按写入顺序保存的日志条目快照。 /// public IReadOnlyList Logs { get { lock (_sync) { return _logs.ToArray(); } } } /// /// 将日志信息追加到内存列表,供断言读取。 /// /// 日志级别。 /// 日志消息。 /// 相关异常;没有异常时为 。 protected override void Write(LogLevel level, string message, Exception? exception) { lock (_sync) { _logs.Add(new LogEntry(level, message, exception)); } } /// /// 表示单个日志条目的不可变快照。 /// /// 日志级别。 /// 日志消息。 /// 相关异常;没有异常时为 。 public sealed record LogEntry(LogLevel Level, string Message, Exception? Exception); }