增加日志

This commit is contained in:
2026-01-16 07:23:56 +08:00
parent 2754cdff15
commit 4e0bb33ce2
11 changed files with 104 additions and 59 deletions

View File

@@ -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 退出前释放所有非托管句柄