增加日志
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
using Serilog;
|
||||
using Serilog.Enrichers.Span; // Nuget: Serilog.Enrichers.Span
|
||||
using Serilog.Events;
|
||||
using Serilog.Exceptions; // Nuget: Serilog.Exceptions
|
||||
using Serilog.Enrichers.Span; // Nuget: Serilog.Enrichers.Span
|
||||
using Serilog.Exceptions.Core;
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
@@ -61,11 +62,19 @@ namespace Ayay.SerilogLogs
|
||||
|
||||
// 2.3 注入全套元数据 (Enrichers) - 让日志更聪明
|
||||
builder
|
||||
// 注入全套元数据 (Enrichers) - 让日志更聪明
|
||||
.Enrich.FromLogContext() // 允许使用 .ForContext() 注入上下文
|
||||
.Enrich.WithProperty("AppId", opts.AppId) // 注入应用标识
|
||||
.Enrich.WithProperty("PcCode", opts.PcCode) // 注入应用标识
|
||||
.Enrich.WithMachineName() // [环境] 区分是哪台工控机 (建议加上)
|
||||
.Enrich.WithThreadId() // 线程ID
|
||||
.Enrich.WithProcessId() // 进程ID (用于识别重启)
|
||||
.Enrich.WithExceptionDetails() // 结构化异常堆栈
|
||||
// [异常] 结构化异常拆解 (非常强大)
|
||||
// 它能把 ex.Data 和 InnerException 自动转成 JSON,而不是单纯的一堆字符串
|
||||
.Enrich.WithExceptionDetails(new DestructuringOptionsBuilder()
|
||||
.WithDefaultDestructurers())
|
||||
//.WithDestructurers(new[] { new SqlExceptionDestructurer() })) // 如果有数据库操作,这行很关键
|
||||
.Enrich.WithSpan(); // 全链路追踪 ID
|
||||
|
||||
// --------------------------------------------------------
|
||||
|
||||
@@ -7,22 +7,24 @@
|
||||
public static class LogModules
|
||||
{
|
||||
// --- 核心架构层 ---
|
||||
public const string Core = "Core"; // 系统主逻辑/启动关闭
|
||||
public const string Network = "Network"; // 底层网络通讯 (TCP/UDP)
|
||||
public const string WebApi = "WebAPI"; // 对外 HTTP 接口
|
||||
public const string WebSocket = "WebSocket"; // 实时通讯
|
||||
public const string Ping = "Ping"; // 心跳/Ping包 (通常量大且不重要)
|
||||
public static string Core { get; } = "Core"; // 系统主逻辑/启动关闭
|
||||
public static string Network { get; } = "Network"; // 底层网络通讯 (TCP/UDP)
|
||||
public static string WebApi { get; } = "WebAPI"; // 对外 HTTP 接口
|
||||
public static string gRpc { get; } = "gRPC"; // 对外 gRPC 接口
|
||||
|
||||
public static string WebSocket { get; } = "WebSocket"; // 实时通讯
|
||||
public static string Ping { get; } = "Ping"; // 心跳/Ping包 (通常量大且不重要)
|
||||
|
||||
// --- 业务逻辑层 ---
|
||||
public const string UserSystem = "UserSystem"; // 用户账户/权限系统
|
||||
public const string UserAction = "UserAction"; // 用户操作行为 (审计日志)
|
||||
public const string DeviceOps = "DeviceOps"; // 设备运行/控制指令
|
||||
public static string UserSystem { get; } = "UserSystem"; // 用户账户/权限系统
|
||||
public static string UserAction { get; } = "UserAction"; // 用户操作行为 (审计日志)
|
||||
public static string DeviceOps { get; } = "DeviceOps"; // 设备运行/控制指令
|
||||
|
||||
// --- 核心算法层 ---
|
||||
public const string Algorithm = "Algorithm"; // 算法分析/AI识别 (需要上下文追踪)
|
||||
public const string Observation = "Observation"; // 观察点/埋点 (用于调试或统计)
|
||||
public static string Algorithm { get; } = "Algorithm"; // 算法分析/AI识别 (需要上下文追踪)
|
||||
public static string Observation { get; } = "Observation"; // 观察点/埋点 (用于调试或统计)
|
||||
|
||||
// --- 第三方集成 ---
|
||||
public const string Sdk = "SDK"; // 第三方 SDK 调用封装
|
||||
public static string HikVisionSdk { get; } = "HikVisionSdk"; // 第三方 SDK 调用封装
|
||||
}
|
||||
}
|
||||
@@ -50,6 +50,11 @@ namespace Ayay.SerilogLogs
|
||||
/// </summary>
|
||||
public string SeqApiKey { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 机器码
|
||||
/// </summary>
|
||||
public string PcCode { get; set; }
|
||||
|
||||
// ==========================================
|
||||
// 4. 输出端级别控制 (Sink Levels)
|
||||
// 用于控制不同媒介的“过滤网”疏密程度
|
||||
@@ -94,23 +99,24 @@ namespace Ayay.SerilogLogs
|
||||
public Dictionary<string, LogEventLevel> ModuleLevels { get; set; } = new Dictionary<string, LogEventLevel>
|
||||
{
|
||||
// --- 系统层 ---
|
||||
{ LogModules.Core, LogEventLevel.Debug }, // 系统主逻辑
|
||||
{ LogModules.Network, LogEventLevel.Debug }, // 网络通讯:平时只看警告,防止心跳刷屏
|
||||
{ LogModules.WebApi, LogEventLevel.Debug }, // WebAPI:记录请求响应
|
||||
{ LogModules.Core, LogEventLevel.Debug }, // 系统主逻辑
|
||||
{ LogModules.Network, LogEventLevel.Debug }, // 网络通讯:平时只看警告,防止心跳刷屏
|
||||
{ LogModules.WebApi, LogEventLevel.Debug }, // WebAPI:记录请求响应
|
||||
{ LogModules.gRpc, LogEventLevel.Debug }, // gRpc:记录请求响应
|
||||
|
||||
// --- 业务层 ---
|
||||
{ LogModules.UserSystem, LogEventLevel.Debug }, // 用户系统
|
||||
{ LogModules.UserAction, LogEventLevel.Debug }, // 用户操作:必须记录,用于审计
|
||||
{ LogModules.DeviceOps, LogEventLevel.Debug }, // 设备操作:记录关键指令
|
||||
{ LogModules.UserSystem, LogEventLevel.Debug }, // 用户系统
|
||||
{ LogModules.UserAction, LogEventLevel.Debug }, // 用户操作:必须记录,用于审计
|
||||
{ LogModules.DeviceOps, LogEventLevel.Debug }, // 设备操作:记录关键指令
|
||||
|
||||
// --- 核心/高频数据 ---
|
||||
{ LogModules.Algorithm, LogEventLevel.Debug }, // 算法:核心业务,开启 Debug 以记录全过程
|
||||
{ LogModules.Observation, LogEventLevel.Debug }, // 观察点:最详细的埋点
|
||||
{ LogModules.Algorithm, LogEventLevel.Debug }, // 算法:核心业务,开启 Debug 以记录全过程
|
||||
{ LogModules.Observation, LogEventLevel.Debug }, // 观察点:最详细的埋点
|
||||
|
||||
// --- 降噪区 (垃圾数据屏蔽) ---
|
||||
{ LogModules.WebSocket, LogEventLevel.Debug }, // WS:数据量极大,除非报错否则不记
|
||||
{ LogModules.Ping, LogEventLevel.Debug }, // Ping:几乎不记,除非完全断连
|
||||
{ LogModules.Sdk, LogEventLevel.Debug } // SDK:屏蔽第三方的废话日志
|
||||
{ LogModules.WebSocket, LogEventLevel.Debug }, // WS:数据量极大,除非报错否则不记
|
||||
{ LogModules.Ping, LogEventLevel.Debug }, // Ping:几乎不记,除非完全断连
|
||||
{ LogModules.HikVisionSdk, LogEventLevel.Debug } // SDK:屏蔽第三方的废话日志
|
||||
};
|
||||
|
||||
// ==========================================
|
||||
|
||||
Reference in New Issue
Block a user