当前位置:网站首页>AspNetCore配置多环境log4net配置文件
AspNetCore配置多环境log4net配置文件
2022-04-23 03:02:00 【dotNET跨平台】
前言
在之前的文章中有讲到AspNetCore多环境配置文件的应用,我们根据自己多种环境分别配置多个appsettings.$EnvironmentName.json
文件。
在实际的开发中我们可能会遇到不只一个配置文件,如当我们使用log4net日志库时,喜欢使用单独的log4net.config
配置文件。并且我们还遇到不同环境下的log4net配置文件还存在差异。这时我们可能可以效仿appsettings.json
多环境配置的风格实现多环境配置文件。
配置log4net
新建Web项目
安装Microsoft.Extensions.Logging.Log4Net.AspNetCore
创建两个环境的配置文件分别如图,并设置文件属性使其在发布时能够复制到根目录
两个配置文件内容如下,例如我们这里生产环境的配置文件多增加一个KafkaAppender
使日志发送至kafka
消息丢列中,然后kafka
的消费者将日志消费至ES集群
,而本地开发的日志则没必要进行上传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>
在Program.cs
中添加log4net配置如下图
// ===============多环境log4net配置===============
IWebHostEnvironment environment = builder.Environment;
var configName = "log4net" + (environment.IsProduction() ? string.Empty : "." + environment.EnvironmentName) + ".config";
builder.Logging.AddLog4Net(configName, watch: true);
// ===============多环境log4net配置===============
现在所有的配置都完成了。
版权声明
本文为[dotNET跨平台]所创,转载请带上原文链接,感谢
https://blog.csdn.net/sd7o95o/article/details/124310718
边栏推荐
- leangoo脑图-共享式多人协作思维导图工具分享
- How can enterprises with major hazard installations ensure the completion of the digital construction task of double prevention mechanism by the end of the year
- 【Hcip】OSPF常用的6种LSA详解
- 《信息系统项目管理师总结》第六章 项目人力资源管理
- JSON data text
- The difference between encodeuri and encodeuricomponent
- Classification and regression tree of machine learning
- Microservices (distributed architecture)
- JS using the parameters of art template
- Résumé du gestionnaire de projet du système d'information Chapitre VI gestion des ressources humaines du projet
猜你喜欢
Linux redis - redis ha sentinel cluster construction details & redis master-slave deployment
Thoughts on the 2022 national network security competition of the national secondary vocational group (only one idea for myself) - network security competition questions (8)
Leangoo brain map - shared multi person collaborative mind mapping tool
How can enterprises with major hazard installations ensure the completion of the digital construction task of double prevention mechanism by the end of the year
Sonic cloud real machine tutorial
Liunx foundation - zabbix5 0 monitoring system installation and deployment
Huawei machine test question -- deformation of hj53 Yang Hui triangle
AC & A2C & A3C
国产轻量级看板式Scrum敏捷项目管理工具
Notes sur le développement de la tarte aux framboises (XII): commencer à étudier la suite UNO - 220 de la tarte aux framboises de contrôle industriel advantech (i): Introduction et fonctionnement du s
随机推荐
Specific field information of MySQL export table (detailed operation of Navicat client)
Introduction and use of openfeign component
Slave should be able to synchronize with the master in tests/integration/replication-psync. tcl
Encapsulation of ele table
Innovation and management based on Scrum
Depth deterministic strategy gradient (ddpg)
tf. keras. layers. Conv? D function
L2-006 树的遍历(中后序确定二叉树&层序遍历)
If MySQL / SQL server judges that the table or temporary table exists, it will be deleted
Q-Learning & Sarsa
HLS / chisel uses CORDIC hyperbolic system to realize square root calculation
Essential qualities of advanced programmers
How can enterprises with major hazard installations ensure the completion of the digital construction task of double prevention mechanism by the end of the year
B blocks of the 46th ICPC Asian regional competition (Kunming)
Summary of interface automation interview questions for software testing
Airtrack cracking wireless network password (Dictionary running method)
The difference between encodeuri and encodeuricomponent
Mosaic Routing: implement / home / news
【Hcip】OSPF常用的6种LSA详解
The space between the left and right of the movie ticket seats is empty and cannot be selected