增加日志
This commit is contained in:
@@ -1,6 +1,11 @@
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Ayay.SerilogLogs;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Serilog;
|
||||
using SHH.CameraSdk;
|
||||
|
||||
/// <summary>
|
||||
/// 设备管理服务引擎 Worker
|
||||
/// </summary>
|
||||
public class CameraEngineWorker : BackgroundService
|
||||
{
|
||||
private readonly CameraManager _manager;
|
||||
@@ -13,17 +18,18 @@ public class CameraEngineWorker : BackgroundService
|
||||
|
||||
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||
{
|
||||
Console.WriteLine("[Engine] 正在启动核心引擎...");
|
||||
var sysLog = Log.ForContext("SourceContext", LogModules.Core);
|
||||
sysLog.Information("[Engine] 正在启动设备管理服务引擎...");
|
||||
|
||||
try
|
||||
{
|
||||
// 1. 理由:启动 SDK 内部加载流程(从本地存储恢复设备)
|
||||
await _manager.StartAsync();
|
||||
Console.WriteLine("[Engine] 设备管理服务已启动。");
|
||||
sysLog.Warning("[Engine] 设备管理服务引擎已启动...");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"[Engine] 严重启动异常: {ex.Message}");
|
||||
sysLog.Error($"[Engine] 设备管理服务引擎启动异常: {ex.Message}");
|
||||
return; // 理由:核心组件失败,终止后续逻辑
|
||||
}
|
||||
|
||||
@@ -31,14 +37,15 @@ public class CameraEngineWorker : BackgroundService
|
||||
while (!stoppingToken.IsCancellationRequested)
|
||||
{
|
||||
// 你可以在这里定期输出一些状态统计
|
||||
// Console.WriteLine($"[Engine] 活跃设备数: {_manager.GetActiveCount()}");
|
||||
sysLog.Debug($"[Engine] 管理设备数: {_manager.GetAllCameras().Count()}");
|
||||
await Task.Delay(TimeSpan.FromSeconds(30), stoppingToken);
|
||||
}
|
||||
}
|
||||
|
||||
public override async Task StopAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
Console.WriteLine("[Engine] 正在执行优雅停机...");
|
||||
var sysLog = Log.ForContext("SourceContext", LogModules.Core);
|
||||
sysLog.Debug($"[Engine] 正在执行优雅停机: {_manager.GetAllCameras()}");
|
||||
try
|
||||
{
|
||||
// 理由:这是重构的核心。必须在 SDK 退出前释放所有非托管句柄
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
using Newtonsoft.Json.Linq;
|
||||
using Ayay.SerilogLogs;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using Serilog;
|
||||
using SHH.Contracts.Grpc;
|
||||
|
||||
namespace SHH.CameraService;
|
||||
@@ -9,6 +11,7 @@ namespace SHH.CameraService;
|
||||
/// </summary>
|
||||
public class CommandDispatcher
|
||||
{
|
||||
private static ILogger _gRpcLog = Log.ForContext("SourceContext", LogModules.gRpc);
|
||||
private readonly Dictionary<string, ICommandHandler> _handlers;
|
||||
|
||||
/// <summary>
|
||||
@@ -32,7 +35,8 @@ public class CommandDispatcher
|
||||
if (protoMsg == null) return;
|
||||
|
||||
string cmdCode = protoMsg.CmdCode; // 例如 "Sync_Camera"
|
||||
Console.WriteLine($"[Dispatcher] 收到远程指令: {cmdCode}, 请求ID: {protoMsg.RequestId}");
|
||||
_gRpcLog.Information($"[gRPC] 响应请求, 业务:{protoMsg.CmdCode}, 请求ID:{protoMsg.RequestId}, 业务分发.");
|
||||
_gRpcLog.Debug($"[gRPC] 响应请求, {protoMsg.CmdCode}, 请求ID:{protoMsg.RequestId}, 业务分发 => {protoMsg}");
|
||||
|
||||
try
|
||||
{
|
||||
@@ -47,16 +51,16 @@ public class CommandDispatcher
|
||||
// 3. 调用具体业务执行
|
||||
await handler.ExecuteAsync(token);
|
||||
|
||||
Console.WriteLine($"[Dispatcher] 指令 {cmdCode} 执行成功。");
|
||||
_gRpcLog.Information($"[gRPC] 业务:{protoMsg.CmdCode}, 请求ID:{protoMsg.RequestId}, 执行成功.");
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine($"[Dispatcher Warning] 未找到指令处理器: {cmdCode}");
|
||||
_gRpcLog.Warning($"[gRPC] 业务:{protoMsg.CmdCode}, 请求ID:{protoMsg.RequestId}, 未找到指令处理器.");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"[Dispatcher Error] 执行指令 {cmdCode} 异常: {ex.Message}");
|
||||
_gRpcLog.Error($"[gRPC] 业务:{protoMsg.CmdCode}, 请求ID:{protoMsg.RequestId}, 执行指令处理异常: {ex.Message}.");
|
||||
}
|
||||
|
||||
// 注意:关于 ACK (require_ack)
|
||||
|
||||
Reference in New Issue
Block a user