多个程序使用同一个 log4net 配置

多个程序使用同一个 log4net 配置,

AssemblyInfo.cs文件加上:

[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", ConfigFileExtension = "config", Watch = true)]
初始化log4net:

log4net.GlobalContext.Properties["program"] = "#your program name#";
log4net.Config.XmlConfigurator.Configure();
调用

protected static readonly ILog SystemLogger = LogManager.GetLogger("SystemLog");
protected static readonly ILog TransferLogger = LogManager.GetLogger("TransferLog");

配置文件 log4net.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<!--log4net config-->
<log4net>
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file value=".\Logs\" /> <!--日志路径-->
<rollingStyle value="Date" /><!--日志滚动模式-->
<datePattern value="_yyyy-MM-dd&quot;.log&quot;" /> <!--日志文件名格式-->
<staticLogFileName value="false" /> <!--日志文件名是否固定不变的-->
<appendToFile value="true" /> <!--是否向文件中追加日志-->
<maximumFileSize value="64MB" /> <!--日志文件最大大小-->
<maxSizeRollBackups value="10" /> <!--日志保留天数-->
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="[%date{HH:mm:ss,fff}][%-5level][%thread][%logger] - %message%newline" /><!--输出格式-->
</layout>

</appender>
<appender name="SystemRollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file type="log4net.Util.PatternString" value=".\Logs\%property{program}\system"/><!--日志路径-->
<rollingStyle value="Date" /><!--日志滚动模式-->
<datePattern value="_yyyy-MM-dd&quot;.log&quot;" /><!--日志文件名格式-->
<staticLogFileName value="false" /> <!--日志文件名是否固定不变的-->
<appendToFile value="true" /><!--是否向文件中追加日志-->
<maximumFileSize value="64MB" /><!--日志文件最大大小-->
<maxSizeRollBackups value="10" /><!--日志保留天数-->
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="[%date{HH:mm:ss,fff}][%-5level][%M]- %message%newline" /><!--输出格式-->
</layout>

<filter type="log4net.Filter.LoggerMatchFilter">
<loggerToMatch value="SystemLog" /><!--过滤器-->
</filter>
<filter type="log4net.Filter.DenyAllFilter" />


</appender>

<appender name="TransferRollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file type="log4net.Util.PatternString" value=".\Logs\%property{program}\transfer"/><!--日志路径-->
<rollingStyle value="Date" /><!--日志滚动模式-->
<datePattern value="_yyyy-MM-dd&quot;.log&quot;" /><!--日志文件名格式-->
<staticLogFileName value="false" /><!--日志文件名是否固定不变的-->
<appendToFile value="true" /><!--是否向文件中追加日志-->
<maximumFileSize value="64MB" /><!--日志文件最大大小-->
<maxSizeRollBackups value="10" /><!--日志保留天数-->
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="[%date{HH:mm:ss,fff}][%-5level][%M]- %message%newline" /><!--输出格式-->
</layout>
<filter type="log4net.Filter.LoggerMatchFilter">
<loggerToMatch value="TransferLog" /><!--过滤器-->
</filter>
<filter type="log4net.Filter.DenyAllFilter" />

</appender>

<appender name="TraceAppender" type="log4net.Appender.TraceAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="[%date] [%level] [%thread] - %message%newline" />
</layout>
</appender>

<appender name="ColoredConsoleAppender" type="log4net.Appender.ColoredConsoleAppender">
<mapping>
<level value="DEBUG" />
<backColor value="Green" />
</mapping>
<mapping>
<level value="INFO" />
<backColor value="Yellow" />
</mapping>
<mapping>
<level value="ERROR" />
<foreColor value="White" />
<backColor value="Red,HighIntensity" />
</mapping>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="[%date][%-5level][%thread][%logger]- %message%newline" />
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<param name="LevelMin" value="Debug" />
<param name="LevelMax" value="Fatal" />
</filter>
</appender>

<root>
<level value="ALL" />
<appender-ref ref="ColoredConsoleAppender" />
<!-- <appender-ref ref="RollingFileAppender" /> -->
<appender-ref ref="SystemRollingFileAppender" />
<appender-ref ref="TransferRollingFileAppender" />
</root>
</log4net>
</configuration>

————————————————
版权声明:本文为CSDN博主「踩可夫司机」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/egoily/article/details/90438736

posted @ 2023-10-18 17:45  China Soft  阅读(75)  评论(0编辑  收藏  举报