增加日志

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,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
// --------------------------------------------------------