当前位置:网站首页>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
边栏推荐
- JS deep copy
- Hand in hand to teach you to use container service tke cluster audit troubleshooting
- Application of V7 version of lvgl Library
- 面试官:缓存穿透、缓存雪崩和缓存击穿是什么?
- Quick for imx6ull development board c program call shell
- How SSL certificate and public IP address affect SEO
- 快来学习!个性化推荐系统开发指南(附网盘链接)
- 技术点5:XML语言
- YoMo Codec - Y3的性能评测报告
- Apache Hadoop的重要组成
猜你喜欢
Another comparison operator related interview question let me understand that the foundation is very important
C/C++编程日记:逻辑井字棋(圈叉)游戏开发
【科创人】Rancher江鹏:从清华工程物理学硕士到云计算开源创业者
Postman(一)---功能介绍
More than 60 technical executives gathered in Songshan Lake, and the first phase of Huawei cloud core partner developer training camp was successfully concluded
IP address SSL certificate
AQS source code in-depth analysis of conditional queue
ES6、ES7、ES8学习指南
2018中国云厂商TOP5:阿里云、腾讯云、AWS、电信、联通 ...
PHP - cURL复制粘贴性接入短信验证码示例
随机推荐
lvgl 库 V7版本相关应用
Can public IP address and SSL certificate improve SEO?
2018中国云厂商TOP5:阿里云、腾讯云、AWS、电信、联通 ...
商品后台系统实现
Another comparison operator related interview question let me understand that the foundation is very important
Operation! Nested JSON second change dataframe!
动物园[CSP2020]
Dongge ate grapes when he ate an algorithm problem!
Problems of input and button (GAP / unequal height / misalignment) and Solutions
白山云科技入选2020中国互联网企业百强
crm系统的成本一般是多少?
More than 60 technical executives gathered in Songshan Lake, and the first phase of Huawei cloud core partner developer training camp was successfully concluded
都要2021年了,现代C++有什么值得我们学习的?
sql 截取数据中 ‘.’的前后数据
How to gracefully prevent switch switching of view UI?
EMQ X 在中国建设银行物联网平台中的应用
团灭 LeetCode 打家劫舍 问题
LeetCode 50 Pow(x,n)
The basic principle of MRAM
说说 C# 9 新特性的实际运用