Serilog 是 .net 里面很是不错的记录日志的库,结构化日志记录,并且配置起来很方便,自定义扩展也很方便html
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.git
Serilog是.NET应用程序的诊断日志库。 它易于设置,具备干净的API,并可在全部最新的.NET平台上运行。 虽然它在最简单的应用程序中也颇有用,但Serilog对结构化日志记录的支持在处理复杂,分布式和异步应用程序和系统时仍然颇有用。github
以前一直使用 log4net 来记录日志,使用 serilog 以后以为 serilog 比 log4net 好用不少,很灵活,配置方式多种多样,支持许多不一样的输出,详细参考 https://github.com/serilog/serilog/wiki/Provided-Sinksapp
最近打算把以前基于 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 很简单很方便,来看示例:async
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 写入到日志中,这样能够方便咱们的请求统计分布式
为了方便使用咱们能够再定义一个扩展方法:ide
public static class EnricherExtensions { public static LoggerConfiguration WithRequestInfo(this LoggerEnrichmentConfiguration enrich) { if (enrich == null) throw new ArgumentNullException(nameof(enrich)); return enrich.With<RequestInfoEnricher>(); } }
配置 Serilog :this
loggingConfig .WriteTo.Elasticsearch(Configuration.GetConnectionString("ElasticSearch"), $"logstash-{ApplicationHelper.ApplicationName.ToLower()}") .Enrich.FromLogContext() .Enrich.WithRequestInfo()
来看一下咱们使用了咱们自定义的 RequestInfoEnricher
以后的日志效果
能够看到咱们自定义的 Enricher 中添加的请求信息已经写到日志里了,并且咱们能够根据 RequestIP 去筛选,为了方便查询 IP 统计,在 kibana 中加了一个可视化面板,效果以下图所示:
原文出处:https://www.cnblogs.com/weihanli/p/custom-serilog-enricher-to-record-more-info.html