当前位置:网站首页>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
边栏推荐
- 一套关于 内存对齐 的C#面试题,做错的人很多!
- 【新版发布】ComponentOne 新增 .NET 6 和 Blazor 平台控件支持
- TP5 multi conditional where query (using PHP variables)
- C# 11 对 ref 和 struct 的改进
- Basic SQL (VIII) data update operation practice
- [Euler plan question 13] sum of large numbers
- 中后二叉建树
- C syntax sugar empty merge operator [?] And null merge assignment operator [? =]
- Thoughts on the 2022 national network security competition of the national secondary vocational group (only one idea for myself) - network security competition questions (10)
- tf. keras. layers. Embedding function
猜你喜欢

ASP.NET 6 中间件系列 - 自定义中间件类

PID debugging of coding motor (speed loop | position loop | follow)

The backtracking of stack is used to solve the problem of "the longest absolute path of file"

HLS / chisel practice CORDIC high performance computing complex square root

TP5 email (2020-05-27)

Xamarin effect Chapter 21 expandable floating operation button in GIS

How to write the expected salary on your resume to double your salary during the interview?

Fight leetcode again (290. Word law)

C# 读写二进制文件

最通俗易懂的依赖注入之服务容器与作用域
随机推荐
中后二叉建树
Judge whether there is a leap year in the given year
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).
svg标签中利用<polygon/>循环数组绘制多边形
[Euler plan question 13] sum of large numbers
求二叉树的叶子结点个数
C read / write binary file
Miniapi of. Net7 (special section): NET7 Preview3
MYSQL05_ Ordr by sorting, limit grouping, group by grouping
Two methods are used to solve the "maximum palindrome product" problem
交换二叉树中每个结点的左和右
ASP.NET和ASP.NETCore多环境配置对比
樹莓派開發筆記(十二):入手研華ADVANTECH工控樹莓派UNO-220套件(一):介紹和運行系統
Laravel new route file
Openfeign timeout setting
編碼電機PID調試(速度環|比特置環|跟隨)
PID debugging of coding motor (speed loop | position loop | follow)
c#语法糖模式匹配【switch 表达式】
HLS / chisel practice CORDIC high performance computing complex square root
Ningde's position in the times is not guaranteed?