加固日志:写入不再静默丢失 + 心跳每 5 分钟落一行
上次现场排查发现 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>
This commit is contained in:
parent
fbcbd80acc
commit
49681b7bdd
@ -83,6 +83,12 @@ public sealed class AgentStatus
|
|||||||
/// <summary>最近一次心跳失败原因(成功则 null)</summary>
|
/// <summary>最近一次心跳失败原因(成功则 null)</summary>
|
||||||
public string? LastHeartbeatError { get; set; }
|
public string? LastHeartbeatError { get; set; }
|
||||||
|
|
||||||
|
/// <summary>累计丢弃的日志行数(日志文件被占用/磁盘满时增长,用于现场判断「日志是否完整」)</summary>
|
||||||
|
public int LogDropped { get; set; }
|
||||||
|
|
||||||
|
/// <summary>最近一次丢日志的时间</summary>
|
||||||
|
public DateTime? LogLastDroppedAt { get; set; }
|
||||||
|
|
||||||
public DateTime UpdatedAt { get; set; } = DateTime.Now;
|
public DateTime UpdatedAt { get; set; } = DateTime.Now;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -26,6 +26,9 @@ public static class AgentHost
|
|||||||
/// <summary>状态写盘保活间隔(内容没变也最多 30 秒写一次)</summary>
|
/// <summary>状态写盘保活间隔(内容没变也最多 30 秒写一次)</summary>
|
||||||
private static readonly TimeSpan StatusKeepAlive = TimeSpan.FromSeconds(30);
|
private static readonly TimeSpan StatusKeepAlive = TimeSpan.FromSeconds(30);
|
||||||
|
|
||||||
|
/// <summary>心跳日志落盘间隔:心跳 60 秒一次,但日志每 5 分钟才写一行(避免刷屏又能看出断点)</summary>
|
||||||
|
private static readonly TimeSpan HeartbeatLogInterval = TimeSpan.FromMinutes(5);
|
||||||
|
|
||||||
private sealed class NtpState
|
private sealed class NtpState
|
||||||
{
|
{
|
||||||
public string LastSyncAt { get; set; } = "";
|
public string LastSyncAt { get; set; } = "";
|
||||||
@ -133,6 +136,8 @@ public static class AgentHost
|
|||||||
string? lastTokenRefreshError = null;
|
string? lastTokenRefreshError = null;
|
||||||
var nextHeartbeatAt = DateTime.MinValue;
|
var nextHeartbeatAt = DateTime.MinValue;
|
||||||
DateTime? lastHeartbeatAt = null;
|
DateTime? lastHeartbeatAt = null;
|
||||||
|
DateTime? lastHeartbeatLoggedAt = null;
|
||||||
|
DateTime? lastHeartbeatFailLoggedAt = null;
|
||||||
string? lastHeartbeatError = null;
|
string? lastHeartbeatError = null;
|
||||||
|
|
||||||
var configTime = AgentFiles.FileTime(AgentPaths.ConfigFile);
|
var configTime = AgentFiles.FileTime(AgentPaths.ConfigFile);
|
||||||
@ -310,12 +315,21 @@ public static class AgentHost
|
|||||||
|
|
||||||
if (lastHeartbeatAt is null)
|
if (lastHeartbeatAt is null)
|
||||||
{
|
{
|
||||||
AppLog.Write("[agent] 设备心跳已开始(每 60 秒)");
|
AppLog.Write("[agent] 设备心跳已开始(每 60 秒,每 5 分钟落一行日志)");
|
||||||
}
|
}
|
||||||
else if (lastHeartbeatError is not null)
|
else if (lastHeartbeatError is not null)
|
||||||
{
|
{
|
||||||
AppLog.Write("[agent] 设备心跳已恢复");
|
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;
|
lastHeartbeatAt = DateTime.Now;
|
||||||
lastHeartbeatError = null;
|
lastHeartbeatError = null;
|
||||||
@ -326,11 +340,15 @@ public static class AgentHost
|
|||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
// 只在第一次失败时记日志,恢复时再记一行,避免每 30 秒刷屏
|
// 首次失败必记;持续失败每 5 分钟补一行,避免刷屏又不至于整段没痕迹
|
||||||
if (lastHeartbeatError is null)
|
if (lastHeartbeatError is null
|
||||||
|
|| lastHeartbeatFailLoggedAt is null
|
||||||
|
|| DateTime.UtcNow - lastHeartbeatFailLoggedAt.Value >= HeartbeatLogInterval)
|
||||||
{
|
{
|
||||||
AppLog.Write($"[agent] 设备心跳失败:{ex.Message}");
|
AppLog.Write($"[agent] 设备心跳失败:{ex.Message}");
|
||||||
|
lastHeartbeatFailLoggedAt = DateTime.UtcNow;
|
||||||
}
|
}
|
||||||
|
|
||||||
lastHeartbeatError = ex.Message;
|
lastHeartbeatError = ex.Message;
|
||||||
|
|
||||||
// 401 = 令牌过期/被撤销,下一轮让续期逻辑先试一下
|
// 401 = 令牌过期/被撤销,下一轮让续期逻辑先试一下
|
||||||
@ -479,6 +497,8 @@ public static class AgentHost
|
|||||||
}).ToList(),
|
}).ToList(),
|
||||||
LastError = ntp.Error,
|
LastError = ntp.Error,
|
||||||
Hik = hik,
|
Hik = hik,
|
||||||
|
LogDropped = AppLog.Dropped,
|
||||||
|
LogLastDroppedAt = AppLog.LastDroppedAt,
|
||||||
NtpLastSyncAt = ntp.LastSyncAt,
|
NtpLastSyncAt = ntp.LastSyncAt,
|
||||||
NtpLastOffsetSeconds = ntp.OffsetSeconds,
|
NtpLastOffsetSeconds = ntp.OffsetSeconds,
|
||||||
NtpRoundTripMs = ntp.RoundTripMs,
|
NtpRoundTripMs = ntp.RoundTripMs,
|
||||||
|
|||||||
@ -39,20 +39,40 @@ public static class AppLog
|
|||||||
|
|
||||||
var now = DateTime.Now;
|
var now = DateTime.Now;
|
||||||
var line = $"[{now:yyyy-MM-dd HH:mm:ss}] {message}{Environment.NewLine}";
|
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;
|
||||||
|
|
||||||
|
/// <summary>累计丢弃的日志行数(写文件一直失败时增长)</summary>
|
||||||
|
public static int Dropped => Volatile.Read(ref _dropped);
|
||||||
|
|
||||||
|
/// <summary>最近一次丢弃日志的时间</summary>
|
||||||
|
public static DateTime? LastDroppedAt { get; private set; }
|
||||||
|
|
||||||
/// <summary>进程内只清理一次(后台服务整天不重启也只跑一次)</summary>
|
/// <summary>进程内只清理一次(后台服务整天不重启也只跑一次)</summary>
|
||||||
private static void EnsureCleanup()
|
private static void EnsureCleanup()
|
||||||
{
|
{
|
||||||
|
|||||||
@ -42,7 +42,7 @@
|
|||||||
<ServerBaseUrl Condition="'$(Configuration)' == 'Debug' and '$(ServerBaseUrl)' == ''">http://127.0.0.1:7300</ServerBaseUrl>
|
<ServerBaseUrl Condition="'$(Configuration)' == 'Debug' and '$(ServerBaseUrl)' == ''">http://127.0.0.1:7300</ServerBaseUrl>
|
||||||
<ServerBaseUrl Condition="'$(Configuration)' == 'Release' and '$(ServerBaseUrl)' == ''">https://packs.example.com</ServerBaseUrl>
|
<ServerBaseUrl Condition="'$(Configuration)' == 'Release' and '$(ServerBaseUrl)' == ''">https://packs.example.com</ServerBaseUrl>
|
||||||
|
|
||||||
<Version>1.0.106</Version>
|
<Version>1.0.107</Version>
|
||||||
<Product>浜戜粨瑙嗛婧簮杞欢</Product>
|
<Product>浜戜粨瑙嗛婧簮杞欢</Product>
|
||||||
<Company>浜戜粨</Company>
|
<Company>浜戜粨</Company>
|
||||||
<AssemblyTitle>浜戜粨瑙嗛婧簮杞欢锛堟湰鏈烘湇鍔★級</AssemblyTitle>
|
<AssemblyTitle>浜戜粨瑙嗛婧簮杞欢锛堟湰鏈烘湇鍔★級</AssemblyTitle>
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user