Serilog 自定义 Enricher 来增加记录的信息
Serilog 自定义 Enricher 来增加记录的信息
Intro#
Serilog 是 .net 里面非常不错的记录日志的库,结构化日志记录,而且配置起来很方便,自定义扩展也很方便
Serilog is a diagnostic logging library for .NET applications. It is easy to set up, has a clean API, and runs on all recent .NET platforms. While it's useful even in the simplest applications, Serilog's support for structured logging shines when instrumenting complex, distributed, and asynchronous applications and systems.
Serilog是.NET应用程序的诊断日志库。 它易于设置,具有干净的API,并可在所有最新的.NET平台上运行。 虽然它在最简单的应用程序中也很有用,但Serilog对结构化日志记录的支持在处理复杂,分布式和异步应用程序和系统时仍然很有用。
之前一直使用 log4net 来记录日志,使用 serilog 之后觉得 serilog 比 log4net 好用很多,很灵活,配置方式多种多样,支持许多不同的输出,详细参考 https://github.com/serilog/serilog/wiki/Provided-Sinks
最近打算把之前基于 log4net 的日志迁移到 serilog, 我自定义的一套 logging 组件也增加了对 Serilog 的支持。 https://www.nuget.org/packages/WeihanLi.Common.Logging.Serilog 现在还没有发布正式版,不过我已经在用了,在等 serilog 发布 2.9.0 正式版,因为 2.8.x 版本的 netstandard2.0 版本还依赖了一个 System.Collections.NonGeneric
,这个依赖只在 netstandard1.3 的时候需要引用,netstandard2.0 已经不需要了,详细信息可以参考PR: https://github.com/serilog/serilog/pull/1342
自定义 Enricher#
自定义 Enricher 很简单很方便,来看示例:
public class RequestInfoEnricher : ILogEventEnricher
{
public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
{
var httpContext = DependencyResolver.Current.GetService<IHttpContextAccessor>()?.HttpContext;
if (null != httpContext)
{
logEvent.AddPropertyIfAbsent(propertyFactory.CreateProperty("RequestIP", httpContext.GetUserIP()));
logEvent.AddPropertyIfAbsent(propertyFactory.CreateProperty("RequestPath", httpContext.Request.Path));
logEvent.AddPropertyIfAbsent(propertyFactory.CreateProperty("Referer", httpContext.Request.Headers["Referer"]));
}
}
}
这个示例会尝试获取请求的 RequestIP/RequestPath/Referer 写入到日志中,这样可以方便我们的请求统计
为了方便使用我们可以再定义一个扩展方法:
public static class EnricherExtensions
{
public static LoggerConfiguration WithRequestInfo(this LoggerEnrichmentConfiguration enrich)
{
if (enrich == null)
throw new ArgumentNullException(nameof(enrich));
return enrich.With<RequestInfoEnricher>();
}
}
配置 Serilog :
loggingConfig
.WriteTo.Elasticsearch(Configuration.GetConnectionString("ElasticSearch"), $"logstash-{ApplicationHelper.ApplicationName.ToLower()}")
.Enrich.FromLogContext()
.Enrich.WithRequestInfo()
验证#
来看一下我们使用了我们自定义的 RequestInfoEnricher
之后的日志效果
可以看到我们自定义的 Enricher 中添加的请求信息已经写到日志里了,而且我们可以根据 RequestIP 去筛选,为了方便查询 IP 统计,在 kibana 中加了一个可视化面板,效果如下图所示:
Reference#
作者:weihanli
出处:https://www.cnblogs.com/weihanli/p/custom-serilog-enricher-to-record-more-info.html
版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
2015-08-08 sqlserver允许远程连接的配置
2015-08-08 Microsoft SQL Server,附加数据库 错误:Error 916解决方法