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
浜戜粨瑙嗛婧簮杞欢
浜戜粨
浜戜粨瑙嗛婧簮杞欢锛堟湰鏈烘湇鍔★級