日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

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

發(fā)布時(shí)間:2023/12/4 编程问答 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Serilog 自定义 Enricher 来增加记录的信息 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

Serilog 自定義 Enricher 來(lái)增加記錄的信息

Intro

Serilog 是 .net 里面非常不錯(cuò)的記錄日志的庫(kù),結(jié)構(gòu)化日志記錄,而且配置起來(lái)很方便,自定義擴(kuò)展也很方便

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應(yīng)用程序的診斷日志庫(kù)。它易于設(shè)置,具有干凈的API,并可在所有最新的.NET平臺(tái)上運(yùn)行。雖然它在最簡(jiǎn)單的應(yīng)用程序中也很有用,但Serilog對(duì)結(jié)構(gòu)化日志記錄的支持在處理復(fù)雜,分布式和異步應(yīng)用程序和系統(tǒng)時(shí)仍然很有用。

之前一直使用 log4net 來(lái)記錄日志,使用 serilog 之后覺(jué)得 serilog 比 log4net 好用很多,很靈活,配置方式多種多樣,支持許多不同的輸出,詳細(xì)參考 https://github.com/serilog/serilog/wiki/Provided-Sinks

最近打算把之前基于 log4net 的日志遷移到 serilog, 我自定義的一套 logging 組件也增加了對(duì) Serilog 的支持。https://www.nuget.org/packages/WeihanLi.Common.Logging.Serilog 現(xiàn)在還沒(méi)有發(fā)布正式版,不過(guò)我已經(jīng)在用了,在等 serilog 發(fā)布 2.9.0 正式版,因?yàn)?2.8.x 版本的 netstandard2.0 版本還依賴(lài)了一個(gè) System.Collections.NonGeneric,這個(gè)依賴(lài)只在 netstandard1.3 的時(shí)候需要引用,netstandard2.0 已經(jīng)不需要了,詳細(xì)信息可以參考PR: https://github.com/serilog/serilog/pull/1342

自定義 Enricher

自定義 Enricher 很簡(jiǎn)單很方便,來(lái)看示例:

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"])); } } }

這個(gè)示例會(huì)嘗試獲取請(qǐng)求的 RequestIP/RequestPath/Referer 寫(xiě)入到日志中,這樣可以方便我們的請(qǐng)求統(tǒng)計(jì)

為了方便使用我們可以再定義一個(gè)擴(kuò)展方法:

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

驗(yàn)證

來(lái)看一下我們使用了我們自定義的 RequestInfoEnricher 之后的日志效果

可以看到我們自定義的 Enricher 中添加的請(qǐng)求信息已經(jīng)寫(xiě)到日志里了,而且我們可以根據(jù) RequestIP 去篩選,為了方便查詢 IP 統(tǒng)計(jì),在 kibana 中加了一個(gè)可視化面板,效果如下圖所示:

Reference

  • https://github.com/serilog/serilog

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

總結(jié)

以上是生活随笔為你收集整理的Serilog 自定义 Enricher 来增加记录的信息的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。