当前位置:网站首页>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]所创,转载请带上原文链接,感谢
边栏推荐
- Markdown plug-in of vscode
- LeetCode 50 Pow(x,n)
- Git old bird search manual
- CUDA_常量内存
- Problems of input and button (GAP / unequal height / misalignment) and Solutions
- 没有磁盘空间 No space left on device
- Important components of Apache Hadoop
- 你了解你的服务器吗、你知道服务器的有哪些内幕吗
- 剑指offer之打印二叉搜索树中第k小的结点
- How can financial management system help enterprises realize financial automation management?
猜你喜欢

必看!RDS 数据库入门一本通(附网盘链接)

Can public IP address and SSL certificate improve SEO?

Operation and design of rights management in ERP

How to carry out modular power operation efficiently

PHP - curl copy paste access SMS verification code example

AQS源码深入分析之条件队列

Open source project, private tool, rapid development

How can financial management system help enterprises realize financial automation management?

SQL server attached database access denial resolution summary

What is the architecture of a live broadcast system worth more than $1 billion?
随机推荐
Low power Bluetooth single chip helps Internet of things
ES6, ES7, es8 Learning Guide
正式班D25
函数计算进阶-IP查询工具开发
Win7 + vs2015 + cuda10.2 configuration tensorrt7.0
Quick for imx6ull development board c program call shell
Performance evaluation report of YoMo codec - Y3
How can financial management system help enterprises realize financial automation management?
How SSL certificate and public IP address affect SEO
Apache Hadoop的重要组成
The basic principle of MRAM
CUDA_存储器模型
Functional guide for temporary users and novices of PL / SQL developer
【QT】子类化QObject+moveToThread实现多线程
LinkedList源码简析
SRM系统是什么系统?SRM供应商管理系统功能
How much is the cost of CRM system?
CUDA_寄存器和局部存储器
快来学习!个性化推荐系统开发指南(附网盘链接)
LeetCode 50 Pow(x,n)