当前位置:网站首页>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]所创,转载请带上原文链接,感谢
边栏推荐
猜你喜欢

How to greatly improve the performance of larravel framework under php7? Install stone!

白山云科技入选2020中国互联网企业百强

手把手教你使用容器服务 TKE 集群审计排查问题

Important components of Apache Hadoop

How to upload your avatar with code and add your own copyright information?

那么当下的行情投资者该怎么办呢? 现在新的投资风口来了!

Come and learn! Development Guide for personalized recommendation system (with internet disk link)

当我们开发一个接口时需要注意些什么

On the practical application of C 9's new features

So what should investors do with the current market? Now a new investment outlet is coming!
随机推荐
[best practice] learn how eolinker helps Telecommuting
Unemployment after graduation? How do college students allocate their study time and have a complete computer knowledge system?
input 与 button 的问题 (空隙/不等高/对不齐)及 解决办法
将Map中对应的key和value赋值到对象中
C/C++编程日记:逻辑井字棋(圈叉)游戏开发
asp.net core中使用Serilog以及自定义Enricher
Open source project, private tool, rapid development
Python提示AttributeError 或者DeprecationWarning: This module was deprecated解决方法
那么当下的行情投资者该怎么办呢? 现在新的投资风口来了!
Hand in hand to teach you to use container service tke cluster audit troubleshooting
The movie theater booking system based on micro Service Framework
Operation! Nested JSON second change dataframe!
Prometheus安装配置
Important components of Apache Hadoop
快来学习!个性化推荐系统开发指南(附网盘链接)
白山云科技入选2020中国互联网企业百强
SQL filter query duplicate columns
Quick for imx6ull development board c program call shell
Software engineering in code -- source code analysis of menu project
获取List集合对象中某一列属性值