大佬教程收集整理的这篇文章主要介绍了Serilog 自定义 Enricher 来增加记录的信息,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
Serilog 是 .net 里面非常不错的记录日志的库,结构化日志记录,而且配置起来很方便,自定义扩展也很方便
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
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()
完整代码示例参考:https://github.com/WeihanLi/ActivityReservation/blob/e68ab090f8b7d660f0a043889f4353551c8b3dc2/ActivityReservation/SerilogEnrichers/requesTinfoEnricher.cs
来看一下我们使用了我们自定义的 requesTinfoEnricher
之后的日志效果
可以看到我们自定义的 Enricher 中添加的请求信息已经写到日志里了,而且我们可以根据 requestIP 去筛选,为了方便查询 IP 统计,在 kibana 中加了一个可视化面板,效果如下图所示:
以上是大佬教程为你收集整理的Serilog 自定义 Enricher 来增加记录的信息全部内容,希望文章能够帮你解决Serilog 自定义 Enricher 来增加记录的信息所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。