当前位置:网站首页>Aspnetcore configuration multi environment log4net configuration file
Aspnetcore configuration multi environment log4net configuration file
2022-04-23 03:07:00 【Dotnet cross platform】
Preface
In the previous article, we talked about AspNetCore Application of multi environment configuration file , We configure multiple according to our various environments appsettings.$EnvironmentName.json
file .
In actual development, we may encounter more than one configuration file , Such as when we use log4net Log library error , Prefer to use separate log4net.config
The configuration file . And we also encounter... In different environments log4net There are also differences in the configuration file . At this time, we may be able to follow appsettings.json
The style of multi environment configuration realizes multi environment configuration file .
To configure log4net
newly build Web project
install Microsoft.Extensions.Logging.Log4Net.AspNetCore
Create two environment configuration files, as shown in Figure , And set the file properties so that it can be copied to the root directory when publishing
The contents of the two configuration files are as follows , For example, we add one more to the configuration file of the production environment KafkaAppender
Send logs to kafka
The message is lost in the column , then kafka
Of consumers consume logs to ES colony
, However, there is no need to upload locally developed logs ES.
<!--log4net.config-->
<?xml version="1.0" encoding="utf-8" ?>
<log4net>
<!-- If you are looking here and want more output, first thing to do is change root/priority/@value to "INFO" or "ALL". -->
<root>
Value of priority may be ALL, DEBUG, INFO, WARN, ERROR, FATAL, OFF.
<priority value="ALL" />
<appender-ref ref="error-file" />
<appender-ref ref="debug-file" />
<appender-ref ref="KafkaAppender" />
</root>
<!-- Example of turning on the output from a component or namespace. -->
<logger name="Common">
<appender-ref ref="debugger"/>
<priority value="DEBUG" />
</logger>
<appender name="KafkaAppender" type="log4net.Kafka.Appender.KafkaAppender, log4net.Kafka.Appender">
<KafkaSettings>
<brokers>
<add value="127.0.0.1:9092" />
</brokers>
<topic type="log4net.Layout.PatternLayout">
<conversionPattern value="kafka.logstash" />
</topic>
</KafkaSettings>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %level% [%t] %logger - %message" />
</layout>
</appender>
<appender name="debugger" type="log4net.Appender.DebugAppender">
<!-- Sends log messages to Visual Studio if attached. -->
<immediateFlush value="true" />
<layout type="log4net.Layout.SimpleLayout" />
</appender>
<appender name="debug-file" type="log4net.Appender.RollingFileAppender">
<param name="Encoding" value="utf-8" />
<file value="Logs/debug" />
<appendToFile value="true" />
<!-- Immediate flush on error log, to avoid data loss with sudden termination. -->
<immediateFlush value="true" />
<staticLogFileName value="false" />
<rollingStyle value="Date" />
<datepattern value="-yyyy.MM.dd'.log'" />
<!-- Prevents Orchard.exe from displaying locking debug messages. -->
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %level% [%property{trace}] %logger - %message%newline" />
</layout>
</appender>
<appender name="error-file" type="log4net.Appender.RollingFileAppender">
<param name="Encoding" value="utf-8" />
<file value="Logs/error" />
<appendToFile value="true" />
<!-- Immediate flush on error log, to avoid data loss with sudden termination. -->
<immediateFlush value="true" />
<staticLogFileName value="false" />
<rollingStyle value="Date" />
<datepattern value="-yyyy.MM.dd'.log'" />
<!-- Prevents Orchard.exe from displaying locking debug messages. -->
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<filter type="log4net.Filter.LevelRangeFilter">
<!-- Only ERROR and FATAL log messages end up in this target, even if child loggers accept lower priority. -->
<levelMin value="ERROR" />
</filter>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%t] %logger - %message [%P{Url}]%newline" />
</layout>
</appender>
</log4net>
<!--log4net.Development.config-->
<?xml version="1.0" encoding="utf-8" ?>
<log4net>
<!-- If you are looking here and want more output, first thing to do is change root/priority/@value to "INFO" or "ALL". -->
<root>
Value of priority may be ALL, DEBUG, INFO, WARN, ERROR, FATAL, OFF.
<priority value="ALL" />
<appender-ref ref="error-file" />
<appender-ref ref="debug-file" />
</root>
<!-- Example of turning on the output from a component or namespace. -->
<logger name="Common">
<appender-ref ref="debugger"/>
<priority value="DEBUG" />
</logger>
<appender name="debugger" type="log4net.Appender.DebugAppender">
<!-- Sends log messages to Visual Studio if attached. -->
<immediateFlush value="true" />
<layout type="log4net.Layout.SimpleLayout" />
</appender>
<appender name="debug-file" type="log4net.Appender.RollingFileAppender">
<param name="Encoding" value="utf-8" />
<file value="Logs/debug" />
<appendToFile value="true" />
<!-- Immediate flush on error log, to avoid data loss with sudden termination. -->
<immediateFlush value="true" />
<staticLogFileName value="false" />
<rollingStyle value="Date" />
<datepattern value="-yyyy.MM.dd'.log'" />
<!-- Prevents Orchard.exe from displaying locking debug messages. -->
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %level% [%property{trace}] %logger - %message%newline" />
</layout>
</appender>
<appender name="error-file" type="log4net.Appender.RollingFileAppender">
<param name="Encoding" value="utf-8" />
<file value="Logs/error" />
<appendToFile value="true" />
<!-- Immediate flush on error log, to avoid data loss with sudden termination. -->
<immediateFlush value="true" />
<staticLogFileName value="false" />
<rollingStyle value="Date" />
<datepattern value="-yyyy.MM.dd'.log'" />
<!-- Prevents Orchard.exe from displaying locking debug messages. -->
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<filter type="log4net.Filter.LevelRangeFilter">
<!-- Only ERROR and FATAL log messages end up in this target, even if child loggers accept lower priority. -->
<levelMin value="ERROR" />
</filter>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%t] %logger - %message [%P{Url}]%newline" />
</layout>
</appender>
</log4net>
stay Program.cs
Add log4net The configuration is as follows
// =============== Multiple environments log4net To configure ===============
IWebHostEnvironment environment = builder.Environment;
var configName = "log4net" + (environment.IsProduction() ? string.Empty : "." + environment.EnvironmentName) + ".config";
builder.Logging.AddLog4Net(configName, watch: true);
// =============== Multiple environments log4net To configure ===============
Now all the configuration is complete .
版权声明
本文为[Dotnet cross platform]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230301333570.html
边栏推荐
- Cherno_ Game engine series tutorial (5): 101~
- Basic SQL (VIII) data update operation practice
- Distributed system services
- 由于3²+4²=5²,所以称‘3,4,5‘为勾股数,求n(包括n)以内所有勾股数数组。
- FileNotFoundError: [Errno 2] No such file or directory
- Two methods are used to solve the "maximum palindrome product" problem
- 如果通过 C# 实现对象的深复制 ?
- Summary of software test interview questions
- Depth deterministic strategy gradient (ddpg)
- C# WPF UI框架MahApps切换主题
猜你喜欢
MAUI初体验:爽
Blazor University (11) component - replace attributes of subcomponents
Openfeign details show
宁德时代地位不保?
OLED多级菜单记录
TP5 email (2020-05-27)
Xamarin效果第二十二篇之录音效果
基于.NetCore开发博客项目 StarBlog - (1) 为什么需要自己写一个博客?
ASP.NET和ASP.NETCore多环境配置对比
Recommend reading | share the trader's book list and ask famous experts for trading advice. The trading is wonderful
随机推荐
FileNotFoundError: [Errno 2] No such file or directory
MySQL port is occupied when building xampp
Due to 3 ²+ four ²= five ², Therefore, we call '3,4,5' as the number of Pythagorean shares, and find the array of all Pythagorean shares within n (including n).
基于.NetCore开发博客项目 StarBlog - (2) 环境准备和创建项目
7-11 rearrange the linked list (25 points)
C#语法糖空合并运算符【??】和空合并赋值运算符【 ??=】
Use DFS to solve the problem of "number of dictionary rows"
tf. keras. layers. Embedding function
Array and collection types passed by openfeign parameters
Depth deterministic strategy gradient (ddpg)
Blazor University (12)组件 — 组件生命周期
[authentication / authorization] customize an authentication handler
AOT和单文件发布对程序性能的影响
微软是如何解决 PC 端程序多开问题的
In redis cluster, the master node fails, and the IP changes after the master-slave switch. The client does not need to deal with it
MYSQL03_ SQL overview, rules and specifications, basic select statements, display table structure
Thoughts on the 2022 national network security competition of the national secondary vocational group (only one idea for myself) - network security competition questions (8)
腾讯视频VIP会员,周卡特价9元!腾讯官方直充,会员立即生效!
ASP. Net and ASP NETCORE multi environment configuration comparison
微软是如何解决 PC 端程序多开问题的——内部实现