增加日志
This commit is contained in:
@@ -1,10 +1,11 @@
|
||||
using System.Net.Sockets;
|
||||
using System.Net;
|
||||
using Ayay.SerilogLogs;
|
||||
using Ayay.SerilogLogs;
|
||||
using Grpc.Net.Client;
|
||||
using Serilog;
|
||||
using SHH.CameraSdk;
|
||||
using SHH.Contracts.Grpc;
|
||||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace SHH.CameraService;
|
||||
|
||||
@@ -37,8 +38,8 @@ public static class Bootstrapper
|
||||
"--uris", "localhost,9001,command,调试PC;",
|
||||
|
||||
// 日志中心配置 (格式: IP,Port,Desc)
|
||||
"--sequris", "172.16.41.241,20026,日志处置中心;",
|
||||
"--seqkey", "Shine899195994250;",
|
||||
"--sequris", "58.216.225.5,20026,日志处置中心;",
|
||||
"--seqkey", "Shine101173874928;",
|
||||
|
||||
// 端口策略
|
||||
"--mode", "1",
|
||||
@@ -48,6 +49,7 @@ public static class Bootstrapper
|
||||
|
||||
var config = ServiceConfig.BuildFromArgs(args);
|
||||
|
||||
string pcCode = config.SeqApiKey.Replace("Shine", "");
|
||||
var ops = new LogOptions
|
||||
{
|
||||
AppId = config.AppId,
|
||||
@@ -56,6 +58,7 @@ public static class Bootstrapper
|
||||
// ★这里改为从 config 读取,如果没配则留空或给个默认值
|
||||
SeqServerUrl = config.SeqServerUrl,
|
||||
SeqApiKey = config.SeqApiKey,
|
||||
PcCode = Regex.Replace(pcCode, ".{3}", "$0-").TrimEnd('-'),
|
||||
|
||||
MaxRetentionDays = 10,
|
||||
FileSizeLimitBytes = 1024L * 1024 * 1024,
|
||||
@@ -234,9 +237,10 @@ public static class Bootstrapper
|
||||
/// <summary>
|
||||
/// 向网关注册实例
|
||||
/// </summary>
|
||||
public static async Task RegisterToGatewayAsync(ServiceConfig config, ILogger logger)
|
||||
public static async Task RegisterToGatewayAsync(ServiceConfig config)
|
||||
{
|
||||
if (!config.CommandEndpoints.Any()) return;
|
||||
var gRpcLog = Log.ForContext("SourceContext", LogModules.gRpc);
|
||||
|
||||
try
|
||||
{
|
||||
@@ -246,7 +250,7 @@ public static class Bootstrapper
|
||||
using var channel = GrpcChannel.ForAddress(targetUrl);
|
||||
var client = new GatewayProvider.GatewayProviderClient(channel);
|
||||
|
||||
logger.Information($"[Grpc] 正在执行预注册: {targetUrl}");
|
||||
gRpcLog.Information($"[gRPC] 正在执行预注册: {targetUrl}");
|
||||
var resp = await client.RegisterInstanceAsync(new RegisterRequest
|
||||
{
|
||||
InstanceId = config.AppId,
|
||||
@@ -257,11 +261,11 @@ public static class Bootstrapper
|
||||
ProcessId = Environment.ProcessId,
|
||||
Description = ""
|
||||
});
|
||||
logger.Information($"💡[Grpc] 预注册成功: {resp.Message}");
|
||||
gRpcLog.Information($"💡[gRPC] 预注册成功: {resp.Message}");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.Error($"⚠️ [Grpc] 预注册尝试失败: {ex.Message}");
|
||||
gRpcLog.Error($"⚠️ [gRPC] 预注册尝试失败: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
using Newtonsoft.Json.Linq;
|
||||
using Ayay.SerilogLogs;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using Serilog;
|
||||
using SHH.CameraSdk;
|
||||
using SHH.Contracts;
|
||||
|
||||
@@ -9,6 +11,7 @@ namespace SHH.CameraService;
|
||||
/// </summary>
|
||||
public class DeviceConfigHandler : ICommandHandler
|
||||
{
|
||||
private static ILogger _sysLog = Log.ForContext("SourceContext", LogModules.Core);
|
||||
private readonly CameraManager _cameraManager;
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
using Grpc.Core;
|
||||
using Ayay.SerilogLogs;
|
||||
using Grpc.Core;
|
||||
using Grpc.Net.Client;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using Serilog;
|
||||
using SHH.CameraSdk;
|
||||
using SHH.Contracts.Grpc; // 引用 Proto 生成的命名空间
|
||||
|
||||
@@ -17,24 +17,23 @@ namespace SHH.CameraService
|
||||
/// </summary>
|
||||
public class GatewayService : BackgroundService
|
||||
{
|
||||
private readonly ILogger<GatewayService> _logger;
|
||||
private readonly ServiceConfig _config;
|
||||
private readonly CommandDispatcher _dispatcher;
|
||||
|
||||
public GatewayService(
|
||||
ILogger<GatewayService> logger,
|
||||
ServiceConfig config,
|
||||
CommandDispatcher dispatcher)
|
||||
{
|
||||
_logger = logger;
|
||||
_config = config;
|
||||
_dispatcher = dispatcher;
|
||||
}
|
||||
|
||||
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||
{
|
||||
var gRpcLog = Log.ForContext("SourceContext", LogModules.gRpc);
|
||||
|
||||
// 预留系统启动缓冲时间,确保数据库和 SDK 已就绪
|
||||
_logger.LogInformation("[gRPC Bus] 指令接收服务启动,等待环境预热...");
|
||||
gRpcLog.Information("[gRPC] 指令接收服务启动,等待环境预热...");
|
||||
await Task.Delay(3000, stoppingToken);
|
||||
|
||||
while (!stoppingToken.IsCancellationRequested)
|
||||
@@ -49,7 +48,7 @@ namespace SHH.CameraService
|
||||
var client = new GatewayProvider.GatewayProviderClient(channel);
|
||||
|
||||
// --- 第一步:发起节点逻辑注册 (Unary) ---
|
||||
_logger.LogInformation("[gRPC Bus] 正在发起逻辑注册: {Url}", targetUrl);
|
||||
gRpcLog.Information("[gRPC] 正在发起逻辑注册: {Url}", targetUrl);
|
||||
var regResp = await client.RegisterInstanceAsync(new RegisterRequest
|
||||
{
|
||||
InstanceId = _config.AppId,
|
||||
@@ -60,7 +59,7 @@ namespace SHH.CameraService
|
||||
|
||||
if (regResp.Success)
|
||||
{
|
||||
_logger.LogInformation("[gRPC Bus] 注册成功。正在建立双向指令通道...");
|
||||
gRpcLog.Information("[gRPC] 注册成功, 正在建立双向指令通道...");
|
||||
|
||||
// --- 第二步:开启 Server Streaming 指令流 ---
|
||||
using var call = client.OpenCommandChannel(new CommandStreamRequest
|
||||
@@ -87,13 +86,14 @@ namespace SHH.CameraService
|
||||
}
|
||||
catch (RpcException ex)
|
||||
{
|
||||
_logger.LogError("[gRPC Bus] RPC 异常 (Status: {Code}): {Msg}", ex.StatusCode, ex.Message);
|
||||
gRpcLog.Debug("[gRPC] RPC 异常 (Status: {Code}): {Msg}", ex.StatusCode, ex.Message);
|
||||
|
||||
// 链路异常,进入重连等待阶段
|
||||
await Task.Delay(5000, stoppingToken);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError("[gRPC Bus] 非预期链路异常: {Msg},5秒后尝试重连", ex.Message);
|
||||
gRpcLog.Debug("[gRPC] 非预期链路异常: {Msg},5秒后尝试重连", ex.Message);
|
||||
await Task.Delay(5000, stoppingToken);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -46,7 +46,7 @@ public class Program
|
||||
config.UpdateActualPort(activePort); // 回填端口
|
||||
|
||||
// 具体的 gRPC 链接逻辑封装在 Bootstrapper 中,保持 Main 清爽但逻辑可见
|
||||
await Bootstrapper.RegisterToGatewayAsync(config, sysLog);
|
||||
await Bootstrapper.RegisterToGatewayAsync(config);
|
||||
|
||||
// =============================================================
|
||||
// 3. 构建 Web 主机环境
|
||||
|
||||
@@ -85,6 +85,8 @@ public static class ServiceCollectionExtensions
|
||||
}
|
||||
|
||||
logger.Information("📋 加载视频流目标: {Count} 个", netTargets.Count);
|
||||
if (netTargets.Count > 0)
|
||||
logger.Debug("🔍 视频流目标详情: {@Targets}", netTargets);
|
||||
|
||||
services.AddSingleton<IEnumerable<StreamTarget>>(netTargets);
|
||||
services.AddHostedService<ImageMonitorController>();
|
||||
|
||||
Reference in New Issue
Block a user