当前位置:网站首页>asp.net core中使用Serilog以及自定义Enricher
asp.net core中使用Serilog以及自定义Enricher
2020-11-09 22:37:00 【程序猿欧文】
Serilog简介
与其他许多.NET库一样,Serilog还提供了对文件,控制台等的基本诊断日志记录。它易于设置,具有简洁的API,并且可以在最新的.NET平台之间移植。
使用和配置、自定义Enricher
由于 serilog 配置信息比较多,避免硬编码代码中,我们在 appsetting.jgon 同级目录中新建一个 serilogsetting.json 的日志配置文件,简单内容如下:
{ "Serilog": { "Using": [ "Serilog.Sinks.Console", "Serilog.Sinks.Seq" ], "MinimumLevel": { "Default": "Information", "Override": { "System": "Error", "Microsoft": "Error" } }, "WriteTo": [ { "Name": "Console" }, { "Name": "Seq", "Args": { "serverUrl": "http://localhost:5341/", "apiKey": "L2on8gpgjose5uldhdch" } } ] }}
更多配置内容请参考:serilog wiki 文档
接下来,在 Program.cs 中添加一个方法,初始化这些配置,然后重写下构建主机的代码:
public class Program{ public static int Main(string[] args) { var logConfig = GetLogConfig(); Log.Logger = new LoggerConfiguration() .ReadFrom.Configuration(logConfig) .Enrich.FormLogContext() .CreateLogger(); try { // web host 初始化 var host = BuildWebHost(args); host.Run(); return 0; } catch(Exception e) { Log.Fatal(ex, "{ApplicationContext} 出现错误:{messsage} !", AppName, ex.Message); return 1; } finally { Log.CloseAndFlush(); } } /// <summary> /// 读取日志配置 /// </summary> /// <returns></returns> private static IConfiguration GetLogConfig() { var builder = new ConfigurationBuilder() .AddJsonFile("serilogsetting.json", optional: false, reloadOnChange: true); return builder.Build(); }}
然后,添加一个自定义Enircher,用来记录HttpContext,的数据。_enrichAction则可以自定义,默认记录ip,请求路径,请求方法,和返回响应码:
public class HttpContextEnricher:ILogEventEnricher{ private readonly IServiceProvider _serviceProvider; private readonly Action<LogEvent, ILogEventPropertyFactory, HttpContext> _enrichAction; public HttpContextEnricher(IServiceProvider serviceProvider) : this(serviceProvider, null) {} public HttpContextEnricher(IServiceProvider serviceProvider, Action<LogEvent, ILogEventPropertyFactory, HttpContext> enrichAction) { _serviceProvider = serviceProvider; if (enrichAction == null) { _enrichAction = (logEvent, pr.........
版权声明
本文为[程序猿欧文]所创,转载请带上原文链接,感谢
https://my.oschina.net/mikeowen/blog/4710338
边栏推荐
- sql 筛选查询重复列
- 配置ng-zerro的nz-date-picker时间选择组件
- 报错 Type interface *** is not known to the MapperRegistry. 的解决方案
- Software engineering in code -- source code analysis of menu project
- Problems of input and button (GAP / unequal height / misalignment) and Solutions
- 【涂鸦物联网足迹】涂鸦云平台接口说明
- LeetCode 49 字母异位词分组
- 【QT】子类化QObject+moveToThread实现多线程
- Another comparison operator related interview question let me understand that the foundation is very important
- [最佳实践]了解 Eolinker 如何助力远程办公
猜你喜欢

迅为IMX6ULL开发板C程序调用shell

More than 60 technical executives gathered in Songshan Lake, and the first phase of Huawei cloud core partner developer training camp was successfully concluded

From master of Engineering Physics of Tsinghua University to open source entrepreneur of cloud computing

报错 Type interface *** is not known to the MapperRegistry. 的解决方案

你了解你的服务器吗、你知道服务器的有哪些内幕吗

sql 截取数据中 ‘.’的前后数据

PHP - curl copy paste access SMS verification code example

Dongge ate grapes when he ate an algorithm problem!

Open source project, private tool, rapid development

Configure the NZ date picker time selection component of ng zerro
随机推荐
lvgl 库 V7版本相关应用
[最佳实践]了解 Eolinker 如何助力远程办公
PHP - cURL复制粘贴性接入短信验证码示例
探访2020 PG技术大会
那么当下的行情投资者该怎么办呢? 现在新的投资风口来了!
快来学习!个性化推荐系统开发指南(附网盘链接)
Configure the NZ date picker time selection component of ng zerro
mongodb内核源码实现、性能调优、最佳运维实践系列-command命令处理模块源码实现一
sql 大小写转换,去掉前后空格
dat.GUI Creating visualization tools (1)
【涂鸦物联网足迹】涂鸦云平台接口说明
团灭 LeetCode 打家劫舍 问题
[最佳实践]了解 Eolinker 如何助力远程办公
剑指offer之打印超过数组一半的数字
JS deep copy
Kubernetes-18: installation and use of dashboard
JT-day10
Performance evaluation report of YoMo codec - Y3
如何优雅阻止view UI 的 Switch 切换?
sql 筛选查询重复列