马上加入IBC程序猿 各种源码随意下,各种教程随便看! 注册 每日签到 加入编程讨论群

C#教程 ASP.NET教程 C#视频教程程序源码享受不尽 C#技术求助 ASP.NET技术求助

【源码下载】 社群合作 申请版主 程序开发 【远程协助】 每天乐一乐 每日签到 【承接外包项目】 面试-葵花宝典下载

官方一群:

官方二群:

Serilog 自定义 Enricher 来增加记录的信息

[复制链接]
查看2063 | 回复1 | 2019-8-13 18:05:16 | 显示全部楼层 |阅读模式

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 很简单很方便,来看示例:

  1. <code>public class RequestInfoEnricher : ILogEventEnricher
  2. {
  3. public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
  4. {
  5. var httpContext = DependencyResolver.Current.GetService<IHttpContextAccessor>()?.HttpContext;
  6. if (null != httpContext)
  7. {
  8. logEvent.AddPropertyIfAbsent(propertyFactory.CreateProperty("RequestIP", httpContext.GetUserIP()));
  9. logEvent.AddPropertyIfAbsent(propertyFactory.CreateProperty("RequestPath", httpContext.Request.Path));
  10. logEvent.AddPropertyIfAbsent(propertyFactory.CreateProperty("Referer", httpContext.Request.Headers["Referer"]));
  11. }
  12. }
  13. }</code>
复制代码

这个示例会尝试获取请求的 RequestIP/RequestPath/Referer 写入到日志中,这样可以方便我们的请求统计

为了方便使用我们可以再定义一个扩展方法:

  1. <code>public static class EnricherExtensions
  2. {
  3. public static LoggerConfiguration WithRequestInfo(this LoggerEnrichmentConfiguration enrich)
  4. {
  5. if (enrich == null)
  6. throw new ArgumentNullException(nameof(enrich));
  7. return enrich.With<RequestInfoEnricher>();
  8. }
  9. }</code>
复制代码

配置 Serilog :

  1. <code>loggingConfig
  2. .WriteTo.Elasticsearch(Configuration.GetConnectionString("ElasticSearch"), $"logstash-{ApplicationHelper.ApplicationName.ToLower()}")
  3. .Enrich.FromLogContext()
  4. .Enrich.WithRequestInfo()</code>
复制代码

完整代码示例参考:https://github.com/WeihanLi/ActivityReservation/blob/e68ab090f8b7d660f0a043889f4353551c8b3dc2/ActivityReservation/SerilogEnrichers/RequestInfoEnricher.cs

验证

来看一下我们使用了我们自定义的 RequestInfoEnricher 之后的日志结果

181003xm6jzcc5k1kjjzke.png

可以看到我们自定义的 Enricher 中添加的请求信息已经写到日志里了,而且我们可以根据 RequestIP 去筛选,为了方便查询 IP 统计,在 kibana 中加了一个可视化面板,结果如下图所示:

181003cxurxnvnpvwss214.png

Reference

  • https://github.com/serilog/serilog
  • https://github.com/WeihanLi/ActivityReservation/blob/e68ab090f8b7d660f0a043889f4353551c8b3dc2/ActivityReservation

来源:https://www.cnblogs.com/weihanli/p/custom-serilog-enricher-to-record-more-info.html
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
*滑块验证:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则