日志补全
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
namespace SHH.CameraSdk;
|
||||
using Serilog;
|
||||
|
||||
namespace SHH.CameraSdk;
|
||||
|
||||
/// <summary>
|
||||
/// [架构基类] 工业级视频源抽象核心 (V3.3.4 严格匹配版)
|
||||
@@ -39,6 +41,8 @@ public abstract class BaseVideoSource : IVideoSource, IAsyncDisposable, IDeviceC
|
||||
}
|
||||
}
|
||||
|
||||
protected abstract ILogger _sdkLog { get; }
|
||||
|
||||
#region --- 1. 核心配置与锁机制 (Core Config & Locks) ---
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -20,7 +20,8 @@ public class HikVideoSource : BaseVideoSource,
|
||||
#region --- 静态资源 (Global Resources) ---
|
||||
|
||||
// 日志实例
|
||||
private static ILogger _sdkLog = Log.ForContext("SourceContext", LogModules.HikVisionSdk);
|
||||
protected override ILogger _sdkLog => Log.ForContext("SourceContext", LogModules.HikVisionSdk);
|
||||
|
||||
// 静态路由表
|
||||
private static readonly ConcurrentDictionary<int, HikVideoSource> _instances = new();
|
||||
// 全局异常回调
|
||||
@@ -108,7 +109,7 @@ public class HikVideoSource : BaseVideoSource,
|
||||
|
||||
if (!HikSdkManager.Initialize())
|
||||
{
|
||||
_sdkLog.Error("HikVision Sdk 初始化失败.");
|
||||
_sdkLog.Error("[SDK] HikVision Sdk 初始化失败.");
|
||||
throw new CameraException(CameraErrorCode.SdkNotInitialized, "HikVision Sdk 初始化失败.", DeviceBrand.HikVision);
|
||||
}
|
||||
|
||||
@@ -117,8 +118,8 @@ public class HikVideoSource : BaseVideoSource,
|
||||
HikNativeMethods.NET_DVR_SetExceptionCallBack_V30(0, IntPtr.Zero, _globalExceptionCallback, IntPtr.Zero);
|
||||
|
||||
// [审计] 记录登录动作
|
||||
AddAuditLog($"Hik 正在执行登录 => ID:{_config.Id} IP:{_config.IpAddress} Port:{_config.Port} Name:{_config.Name}");
|
||||
_sdkLog.Information($"Hik 正在执行登录 => ID:{_config.Id} IP:{_config.IpAddress} Port:{_config.Port} Name:{_config.Name}");
|
||||
_sdkLog.Information($"[SDK] Hik 正在执行登录 => ID:{_config.Id} IP:{_config.IpAddress} Port:{_config.Port} Name:{_config.Name}");
|
||||
AddAuditLog($"[SDK] Hik 正在执行登录 => ID:{_config.Id} IP:{_config.IpAddress} Port:{_config.Port} Name:{_config.Name}");
|
||||
|
||||
var devInfo = new HikNativeMethods.NET_DEVICEINFO_V30();
|
||||
int newUserId = HikNativeMethods.NET_DVR_Login_V30(
|
||||
@@ -138,7 +139,9 @@ public class HikVideoSource : BaseVideoSource,
|
||||
}
|
||||
|
||||
_instances.TryAdd(_userId, this);
|
||||
AddAuditLog($"物理登录成功 (UserID: {_userId})");
|
||||
|
||||
_sdkLog.Information($"[SDK] Hik 登录成功 => ID:{_config.Id} IP:{_config.IpAddress} Port:{_config.Port} Name:{_config.Name}, UserID: {_userId}");
|
||||
AddAuditLog($"[SDK] Hik 登录成功 => ID:{_config.Id} IP:{_config.IpAddress} Port:{_config.Port} Name:{_config.Name}, UserID: {_userId}");
|
||||
|
||||
// 开启取流
|
||||
if (!StartRealPlay())
|
||||
@@ -147,11 +150,13 @@ public class HikVideoSource : BaseVideoSource,
|
||||
throw new CameraException(HikErrorMapper.Map(err), $"预览失败: {err}", DeviceBrand.HikVision, (int)err);
|
||||
}
|
||||
|
||||
AddAuditLog($"网络取流成功 (StreamType: {_config.StreamType})");
|
||||
_sdkLog.Debug($"[SDK] Hik 网络取流成功 => ID:{_config.Id} IP:{_config.IpAddress} Port:{_config.Port} Name:{_config.Name}, UserID: {_userId}");
|
||||
AddAuditLog($"[SDK] Hik 网络取流成功 => ID:{_config.Id} IP:{_config.IpAddress} Port:{_config.Port} Name:{_config.Name}, UserID: {_userId}");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
AddAuditLog($"启动异常: {ex.Message}");
|
||||
_sdkLog.Error($"[SDK] Hik 启动异常. => ID:{_config.Id} IP:{_config.IpAddress} Port:{_config.Port} Name:{_config.Name}, UserID: {_userId}");
|
||||
AddAuditLog($"[SDK] Hik 启动异常. => ID:{_config.Id} IP:{_config.IpAddress} Port:{_config.Port} Name:{_config.Name}, UserID: {_userId}");
|
||||
CleanupSync();
|
||||
throw;
|
||||
}
|
||||
@@ -161,9 +166,14 @@ public class HikVideoSource : BaseVideoSource,
|
||||
protected override async Task OnStopAsync()
|
||||
{
|
||||
Interlocked.Increment(ref _connectionEpoch);
|
||||
AddAuditLog("正在执行停止流程...");
|
||||
|
||||
_sdkLog.Debug($"[SDK] Hik 正在执行停止流程. => ID:{_config.Id} IP:{_config.IpAddress} Port:{_config.Port} Name:{_config.Name}, UserID: {_userId}");
|
||||
AddAuditLog($"[SDK] Hik 正在执行停止流程. => ID:{_config.Id} IP:{_config.IpAddress} Port:{_config.Port} Name:{_config.Name}, UserID: {_userId}");
|
||||
|
||||
await Task.Run(() => CleanupSync());
|
||||
AddAuditLog("设备已停止");
|
||||
|
||||
_sdkLog.Information($"[SDK] Hik 设备已停止. => ID:{_config.Id} IP:{_config.IpAddress} Port:{_config.Port} Name:{_config.Name}, UserID: {_userId}");
|
||||
AddAuditLog($"[SDK] Hik 设备已停止. => ID:{_config.Id} IP:{_config.IpAddress} Port:{_config.Port} Name:{_config.Name}, UserID: {_userId}");
|
||||
}
|
||||
|
||||
private void CleanupSync()
|
||||
@@ -218,6 +228,8 @@ public class HikVideoSource : BaseVideoSource,
|
||||
if (options.StreamType.HasValue)
|
||||
{
|
||||
int targetStream = options.StreamType.Value;
|
||||
|
||||
_sdkLog.Debug($"[SDK] Hik 收到码流切换请求. => ID:{_config.Id} IP:{_config.IpAddress} Port:{_config.Port} Name:{_config.Name}, UserID: {_userId}, 播放句柄:{_realPlayHandle}");
|
||||
AddAuditLog($"收到码流切换请求: {targetStream},开始执行热切换...");
|
||||
|
||||
lock (_initLock)
|
||||
@@ -244,12 +256,14 @@ public class HikVideoSource : BaseVideoSource,
|
||||
// D. 重新开启预览
|
||||
if (StartRealPlay())
|
||||
{
|
||||
AddAuditLog($"码流热切换成功 (当前: {(_config.StreamType == 0 ? "主" : "子")}码流)");
|
||||
_sdkLog.Information($"[SDK] Hik 码流热切换成功. => 当前: {(_config.StreamType == 0 ? "主" : "子")}码流), ID:{_config.Id} IP:{_config.IpAddress} Port:{_config.Port} Name:{_config.Name}, UserID: {_userId}, 播放句柄:{_realPlayHandle}");
|
||||
AddAuditLog($"[SDK] Hik 码流热切换成功. => 当前: {(_config.StreamType == 0 ? "主" : "子")}码流), ID:{_config.Id} IP:{_config.IpAddress} Port:{_config.Port} Name:{_config.Name}, UserID: {_userId}, 播放句柄:{_realPlayHandle}");
|
||||
}
|
||||
else
|
||||
{
|
||||
uint err = HikNativeMethods.NET_DVR_GetLastError();
|
||||
AddAuditLog($"码流切换失败: {err}");
|
||||
_sdkLog.Information($"[SDK] Hik 码流切换失败. => Err:{err}, ID:{_config.Id} IP:{_config.IpAddress} Port:{_config.Port} Name:{_config.Name}, UserID: {_userId}, 播放句柄:{_realPlayHandle}");
|
||||
AddAuditLog($"[SDK] Hik 码流切换失败. => Err:{err}, ID:{_config.Id} IP:{_config.IpAddress} Port:{_config.Port} Name:{_config.Name}, UserID: {_userId}, 播放句柄:{_realPlayHandle}");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -258,7 +272,8 @@ public class HikVideoSource : BaseVideoSource,
|
||||
if (options.RenderHandle.HasValue)
|
||||
{
|
||||
// 如果是硬解码模式,可以在这里调用 PlayM4_Play(port, newHandle)
|
||||
AddAuditLog($"收到新句柄绑定请求: {options.RenderHandle}");
|
||||
_sdkLog.Information($"[SDK] Hik 收到新句柄绑定请求, 新句柄:{options.RenderHandle}. => ID:{_config.Id} IP:{_config.IpAddress} Port:{_config.Port} Name:{_config.Name}, UserID: {_userId}, 播放句柄:{_realPlayHandle}");
|
||||
AddAuditLog($"[SDK] Hik 收到新句柄绑定请求, 新句柄:{options.RenderHandle}. => ID:{_config.Id} IP:{_config.IpAddress} Port:{_config.Port} Name:{_config.Name}, UserID: {_userId}, 播放句柄:{_realPlayHandle}");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -425,7 +440,10 @@ public class HikVideoSource : BaseVideoSource,
|
||||
{
|
||||
if (_instances.TryGetValue(lUserID, out var instance))
|
||||
{
|
||||
instance.AddAuditLog($"SDK报警异常: 0x{dwType:X}"); // 写入审计
|
||||
Log.ForContext("SourceContext", LogModules.HikVisionSdk)
|
||||
.Error($"Hik SDK 报警异常: 0x{dwType:X}, UserId: {lUserID} ");
|
||||
|
||||
instance.AddAuditLog($"SDK报警 User:{pUser} 异常: 0x{dwType:X}, UserId: {lUserID}"); // 写入审计
|
||||
instance.ReportError(new CameraException(
|
||||
CameraErrorCode.NetworkUnreachable,
|
||||
$"SDK全局异常: 0x{dwType:X}",
|
||||
|
||||
Reference in New Issue
Block a user