using System;
using System.Collections.Generic;
namespace YuncangTrace.LocalService.Services;
///
/// 进程级日志(托盘 / 登录窗 / 主窗 / HTTP 服务都往这里写)
///
/// 服务在登录之前就起来了,那时主窗口还不存在 —— 所以日志要有个全局缓冲,
/// 主窗口打开时用 把历史补上。
///
public static class AppLog
{
private const int MaxLines = 300;
private static readonly object Gate = new();
private static readonly Queue Buffer = new();
/// 新日志(订阅方负责切回 UI 线程)
public static event Action? Written;
/// 写入一行日志
public static void Write(string message)
{
var line = $"[{DateTime.Now:HH:mm:ss}] {message}";
lock (Gate)
{
Buffer.Enqueue(line);
while (Buffer.Count > MaxLines) Buffer.Dequeue();
}
Written?.Invoke(line);
}
/// 最近的历史日志(窗口初始化时灌进去)
public static IReadOnlyList History
{
get
{
lock (Gate)
{
return Buffer.ToArray();
}
}
}
}