当前位置:网站首页>asp.net Using serilog in core and customizing enrich
asp.net Using serilog in core and customizing enrich
2020-11-09 22:37:00 【Irving the procedural ape】
Serilog brief introduction
With many others .NET Like the library ,Serilog Also provided are the documents , Basic diagnostic logging of console, etc . It's easy to set up , Have a simple API, And can be in the latest .NET Porting between platforms .
Use and configure 、 Customize Enricher
because serilog There is a lot of configuration information , Avoid hard coding , We are appsetting.jgon Create a new one in the peer directory serilogsetting.json Log profile for , The simple content is as follows :
{ "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" } } ] }}
Please refer to :serilog wiki file
Next , stay Program.cs Add a method to , Initialize these configurations , And then rewrite the code that builds the host :
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 initialization var host = BuildWebHost(args); host.Run(); return 0; } catch(Exception e) { Log.Fatal(ex, "{ApplicationContext} There is an error :{messsage} !", AppName, ex.Message); return 1; } finally { Log.CloseAndFlush(); } } /// <summary> /// Read log configuration /// </summary> /// <returns></returns> private static IConfiguration GetLogConfig() { var builder = new ConfigurationBuilder() .AddJsonFile("serilogsetting.json", optional: false, reloadOnChange: true); return builder.Build(); }}
then , Add a custom Enircher, Used to record HttpContext, The data of ._enrichAction Then you can customize , Default record ip, Request path , Request method , And return response code :
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.........
版权声明
本文为[Irving the procedural ape]所创,转载请带上原文链接,感谢
边栏推荐
- Make a home page
- LeetCode 50 Pow(x,n)
- C/C++编程笔记:C语言开发坦克大战!纪念我们逝去的小霸王游戏
- Error running app:Default Activity not found 解决方法
- 在PHP7下怎么大幅度提升Laravel框架性能?安装Stone!
- PHP - curl copy paste access SMS verification code example
- Postman(一)---功能介绍
- Low power Bluetooth single chip helps Internet of things
- crm系统的成本一般是多少?
- Do you know your server? Do you know what's inside the server
猜你喜欢
Optimization of commodity backstage system
From master of Engineering Physics of Tsinghua University to open source entrepreneur of cloud computing
crm系统的成本一般是多少?
Analysis steps of commodity background management
那么当下的行情投资者该怎么办呢? 现在新的投资风口来了!
Jingtao project Day10
expect ':' at 0, actual = (JSON转化异常解决)
Important components of Apache Hadoop
如何k个一组反转链表
How to make a set of K reverse linked lists
随机推荐
The solution of type interface * * is not known to the mapperregistry
Configure the NZ date picker time selection component of ng zerro
剑指offer之打印二叉搜索树中第k小的结点
Formal class D25
Visit 2020 PG Technology Conference
eleven point nine
crm系统的成本一般是多少?
sql 大小写转换,去掉前后空格
From master of Engineering Physics of Tsinghua University to open source entrepreneur of cloud computing
No space left on device
毕业即失业?大学生如何分配学习时间比例,拥有完整计算机知识体系?
[graffiti Internet of things footprint] graffiti cloud platform interface description
刚毕业都会迷茫,我经过7年总结,送给程序员的你们7点建议
So what should investors do with the current market? Now a new investment outlet is coming!
What is the architecture of a live broadcast system worth more than $1 billion?
爱康国宾怒斥国信证券报告失实,已发律师函
那么当下的行情投资者该怎么办呢? 现在新的投资风口来了!
商品后台系统实现
lvgl 库 V7版本相关应用
IP address SSL certificate