From 49681b7bddab994d5a4aa4f492dcc0916c7cbd2c Mon Sep 17 00:00:00 2001 From: liufumingren Date: Thu, 22 Oct 2026 03:12:14 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8A=A0=E5=9B=BA=E6=97=A5=E5=BF=97=EF=BC=9A?= =?UTF-8?q?=E5=86=99=E5=85=A5=E4=B8=8D=E5=86=8D=E9=9D=99=E9=BB=98=E4=B8=A2?= =?UTF-8?q?=E5=A4=B1=20+=20=E5=BF=83=E8=B7=B3=E6=AF=8F=205=20=E5=88=86?= =?UTF-8?q?=E9=92=9F=E8=90=BD=E4=B8=80=E8=A1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 上次现场排查发现 16:28-22:47 六小时日志全丢,但同一时间上报和 scan-records.json 都正常 —— 原因是 File.AppendAllText 按 FileShare.Read 打开,日志文件被别的程序占着时每次都抛 IOException,又被 catch{} 吞掉。 - AppLog 改用 FileShare.ReadWrite|Delete 打开并重试一次,仍失败则累加丢弃计数 - agent-status.json 暴露 LogDropped / LogLastDroppedAt,现场能看出日志是否完整 - 心跳日志改为每 5 分钟一行(含在线通道数、待上传条数),持续失败也每 5 分钟补一行 - 通道断开日志沿用原有节流(前 3 次 + 每 10 次),配合心跳行可定位断点 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- desktop/Services/Agent/AgentFiles.cs | 6 +++++ desktop/Services/Agent/AgentHost.cs | 26 +++++++++++++++--- desktop/Services/AppLog.cs | 34 +++++++++++++++++++----- desktop/YuncangTrace.LocalService.csproj | 2 +- 4 files changed, 57 insertions(+), 11 deletions(-) diff --git a/desktop/Services/Agent/AgentFiles.cs b/desktop/Services/Agent/AgentFiles.cs index 861125e..7a2da7f 100644 --- a/desktop/Services/Agent/AgentFiles.cs +++ b/desktop/Services/Agent/AgentFiles.cs @@ -83,6 +83,12 @@ public sealed class AgentStatus /// 最近一次心跳失败原因(成功则 null) public string? LastHeartbeatError { get; set; } + /// 累计丢弃的日志行数(日志文件被占用/磁盘满时增长,用于现场判断「日志是否完整」) + public int LogDropped { get; set; } + + /// 最近一次丢日志的时间 + public DateTime? LogLastDroppedAt { get; set; } + public DateTime UpdatedAt { get; set; } = DateTime.Now; } diff --git a/desktop/Services/Agent/AgentHost.cs b/desktop/Services/Agent/AgentHost.cs index cc2ab16..34df6b5 100644 --- a/desktop/Services/Agent/AgentHost.cs +++ b/desktop/Services/Agent/AgentHost.cs @@ -26,6 +26,9 @@ public static class AgentHost /// 状态写盘保活间隔(内容没变也最多 30 秒写一次) private static readonly TimeSpan StatusKeepAlive = TimeSpan.FromSeconds(30); + /// 心跳日志落盘间隔:心跳 60 秒一次,但日志每 5 分钟才写一行(避免刷屏又能看出断点) + private static readonly TimeSpan HeartbeatLogInterval = TimeSpan.FromMinutes(5); + private sealed class NtpState { public string LastSyncAt { get; set; } = ""; @@ -133,6 +136,8 @@ public static class AgentHost string? lastTokenRefreshError = null; var nextHeartbeatAt = DateTime.MinValue; DateTime? lastHeartbeatAt = null; + DateTime? lastHeartbeatLoggedAt = null; + DateTime? lastHeartbeatFailLoggedAt = null; string? lastHeartbeatError = null; var configTime = AgentFiles.FileTime(AgentPaths.ConfigFile); @@ -310,12 +315,21 @@ public static class AgentHost if (lastHeartbeatAt is null) { - AppLog.Write("[agent] 设备心跳已开始(每 60 秒)"); + AppLog.Write("[agent] 设备心跳已开始(每 60 秒,每 5 分钟落一行日志)"); } else if (lastHeartbeatError is not null) { AppLog.Write("[agent] 设备心跳已恢复"); } + else if (lastHeartbeatLoggedAt is null + || DateTime.UtcNow - lastHeartbeatLoggedAt.Value >= HeartbeatLogInterval) + { + // 心跳本身 60 秒一次,但只在每 5 分钟落一行,方便隔天回看「几点断的」 + var online = scan.Channels.Count(item => item.Connected); + AppLog.Write($"[agent] 心跳正常(在线通道 {online}/{scan.Channels.Count}," + + $"待上传 {ScanRecordStore.PendingCount()} 条)"); + lastHeartbeatLoggedAt = DateTime.UtcNow; + } lastHeartbeatAt = DateTime.Now; lastHeartbeatError = null; @@ -326,11 +340,15 @@ public static class AgentHost } catch (Exception ex) { - // 只在第一次失败时记日志,恢复时再记一行,避免每 30 秒刷屏 - if (lastHeartbeatError is null) + // 首次失败必记;持续失败每 5 分钟补一行,避免刷屏又不至于整段没痕迹 + if (lastHeartbeatError is null + || lastHeartbeatFailLoggedAt is null + || DateTime.UtcNow - lastHeartbeatFailLoggedAt.Value >= HeartbeatLogInterval) { AppLog.Write($"[agent] 设备心跳失败:{ex.Message}"); + lastHeartbeatFailLoggedAt = DateTime.UtcNow; } + lastHeartbeatError = ex.Message; // 401 = 令牌过期/被撤销,下一轮让续期逻辑先试一下 @@ -479,6 +497,8 @@ public static class AgentHost }).ToList(), LastError = ntp.Error, Hik = hik, + LogDropped = AppLog.Dropped, + LogLastDroppedAt = AppLog.LastDroppedAt, NtpLastSyncAt = ntp.LastSyncAt, NtpLastOffsetSeconds = ntp.OffsetSeconds, NtpRoundTripMs = ntp.RoundTripMs, diff --git a/desktop/Services/AppLog.cs b/desktop/Services/AppLog.cs index 672a4e0..34f265c 100644 --- a/desktop/Services/AppLog.cs +++ b/desktop/Services/AppLog.cs @@ -39,20 +39,40 @@ public static class AppLog var now = DateTime.Now; var line = $"[{now:yyyy-MM-dd HH:mm:ss}] {message}{Environment.NewLine}"; + var path = Path.Combine(LogDirectory, FileNameOf(now)); - try + // 不要用 File.AppendAllText:它按 FileShare.Read 打开,只要日志文件正被 + // 记事本/编辑器/日志工具独占打开,每次写入都会失败(2026-10-21 现场丢了 6 小时日志)。 + // 这里显式 FileShare.ReadWrite,并重试一次;仍然失败就记一次丢弃计数, + // 由 agent-status.json 暴露出去,避免「日志没了还查不出」。 + for (var attempt = 0; attempt < 2; attempt++) { - lock (Gate) + try { - File.AppendAllText(Path.Combine(LogDirectory, FileNameOf(now)), line, Utf8NoBom); + using var stream = new FileStream(path, FileMode.Append, FileAccess.Write, + FileShare.ReadWrite | FileShare.Delete); + var bytes = Utf8NoBom.GetBytes(line); + stream.Write(bytes, 0, bytes.Length); + return; + } + catch + { + if (attempt == 0) Thread.Sleep(20); } } - catch - { - // 磁盘满 / 无权限:日志丢了也不能影响功能 - } + + Interlocked.Increment(ref _dropped); + LastDroppedAt = DateTime.Now; } + private static int _dropped; + + /// 累计丢弃的日志行数(写文件一直失败时增长) + public static int Dropped => Volatile.Read(ref _dropped); + + /// 最近一次丢弃日志的时间 + public static DateTime? LastDroppedAt { get; private set; } + /// 进程内只清理一次(后台服务整天不重启也只跑一次) private static void EnsureCleanup() { diff --git a/desktop/YuncangTrace.LocalService.csproj b/desktop/YuncangTrace.LocalService.csproj index fdec01f..a5d656c 100644 --- a/desktop/YuncangTrace.LocalService.csproj +++ b/desktop/YuncangTrace.LocalService.csproj @@ -42,7 +42,7 @@ http://127.0.0.1:7300 https://packs.example.com - 1.0.106 + 1.0.107 浜戜粨瑙嗛婧簮杞欢 浜戜粨 浜戜粨瑙嗛婧簮杞欢锛堟湰鏈烘湇鍔★級