当前位置:网站首页>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
边栏推荐
- Leetcode 48 rotating image
- ES6, ES7, es8 Learning Guide
- 正式班D25
- JS深拷贝
- Operation! Nested JSON second change dataframe!
- 面试官:缓存穿透、缓存雪崩和缓存击穿是什么?
- [best practice] learn how eolinker helps Telecommuting
- CRM系统能帮助企业做哪些事?
- Software engineering in code -- source code analysis of menu project
- Hand in hand to teach you to use container service tke cluster audit troubleshooting
猜你喜欢
How to carry out modular power operation efficiently
Win7 + vs2015 + cuda10.2 configuration tensorrt7.0
PLSQL Developer常用设置
毕业即失业?大学生如何分配学习时间比例,拥有完整计算机知识体系?
dat.GUI Creating visualization tools (1)
如何实现LRU算法
Low power Bluetooth single chip helps Internet of things
当我们开发一个接口时需要注意些什么
SRM系统是什么系统?SRM供应商管理系统功能
Git old bird search manual
随机推荐
Open source project, private tool, rapid development
商品后台管理实现分析步骤
PHP - curl copy paste access SMS verification code example
开源项目,私活利器,快速开发
sql 截取数据中 ‘.’的前后数据
How to gracefully prevent switch switching of view UI?
配置ng
东哥吃葡萄时竟然吃出一道算法题!
Leetcode 49 letter heterotopic word grouping
获取List集合对象中某一列属性值
【QT】子类化QObject+moveToThread实现多线程
Unemployment after graduation? How do college students allocate their study time and have a complete computer knowledge system?
VsCode之Markdown插件
迅为IMX6ULL开发板C程序调用shell
Chrome扩展程序热更新方案:2.基于双缓存更新功能模块
[最佳实践]了解 Eolinker 如何助力远程办公
SQL server attached database access denial resolution summary
PHP - cURL复制粘贴性接入短信验证码示例
How SSL certificate and public IP address affect SEO
财务管理系统如何帮助企业实现财务自动化管理?