如何把 log4net.config 文件改成代码?
这样在编写winform或者其他客户端的时候就省去了一个配置文件
分享下我常用的log4net.config 配置文件
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<!--<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/>
</configSections>-->
<log4net>
<!--console-->
<!--<appender name="RollingFileAppender_Debug" type="log4net.Appender.ManagedColoredConsoleAppender">
<encoding value="utf-8"/>
<mapping>
<level value="ERROR" />
<foreColor value="Red" />
</mapping>
<mapping>
<level value="DEBUG" />
<foreColor value="Gray" />
</mapping>
<mapping>
<level value="Info" />
<foreColor value="Yellow" />
</mapping>
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />
</layout>
</appender>-->
<!-- RollingFileAppender to log the messages that their level are DEBUG and INFO -->
<appender name="RollingFileAppender_Debug" type="log4net.Appender.RollingFileAppender">
<file value="Logs/Debug/" />
<encoding value="utf-8"/>
<appendToFile value="true" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="100MB" />
<rollingStyle value="Composite" />
<datePattern value="yyyy-MM-dd.'log'" />
<staticLogFileName value="false" />
<!-- Set filter,LevelRangeFilter -->
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="INFO" />
<levelMax value="INFO" />
</filter>
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="[级别:%p]%n[时间:%d]%n[类名:%c]%n[方法:%M]%n[行号:%L]%n[内容:%m]%n------------------------------------------------------------------------------------------------%n%n" />
</layout>
</appender>
<!-- RollingFileAppender to log the messages that their level are DEBUG and INFO -->
<appender name="RollingFileAppender_Info" type="log4net.Appender.RollingFileAppender">
<file value="Logs/Info/" />
<encoding value="utf-8"/>
<appendToFile value="true" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="100MB" />
<rollingStyle value="Composite" />
<datePattern value="yyyy-MM-dd.'log'" />
<staticLogFileName value="false" />
<!-- Set filter,LevelRangeFilter -->
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="INFO" />
<levelMax value="INFO" />
</filter>
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="[%d]%m%n" />
</layout>
</appender>
<!-- RollingFileAppender to log the messages that their level are WARN or higher -->
<appender name="RollingFileAppender_Error" type="log4net.Appender.RollingFileAppender">
<file value="Logs/Error/" />
<encoding value="utf-8"/>
<appendToFile value="true" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="20MB" />
<rollingStyle value="Composite" />
<datePattern value="yyyy-MM-dd.'log'" />
<staticLogFileName value="false" />
<!-- Set filter,LevelRangeFilter -->
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="ERROR" />
<levelMax value="FATAL" />
</filter>
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="[级别:%p]%n[时间:%d]%n[类名:%c]%n[方法:%M]%n[行号:%L]%n[内容:%m]%n[异常:%exception]%n------------------------------------------------------------------------------------------------%n%n" />
</layout>
</appender>
<!-- Setup the root category, add the appenders and set the default level -->
<root>
<level value="ALL" />
<!--<appender-ref ref="RollingFileAppender_Debug" />-->
<appender-ref ref="RollingFileAppender_Info" />
<appender-ref ref="RollingFileAppender_Error" />
</root>
</log4net>
</configuration>
只需要在程序启动的时候执行下下面的代码,其他代码不变就可以直接使用了.
var hierarchy = (Hierarchy)LogManager.GetRepository();
var infoAppender = new RollingFileAppender
{
Name = "RollingFileAppender_Info",
File = $"Logs/Info/",
Encoding = System.Text.Encoding.UTF8,
AppendToFile = true,
MaxSizeRollBackups = 10,
MaximumFileSize = "10MB",
RollingStyle = RollingFileAppender.RollingMode.Composite,
DatePattern = "yyyy-MM-dd'.log'",
StaticLogFileName = false
};
var infoFilter = new LevelRangeFilter
{
LevelMin = Level.Info,
LevelMax = Level.Info
};
infoAppender.AddFilter(infoFilter);
var infoLayout = new PatternLayout
{
ConversionPattern = "[%date]%message%newline"
};
infoLayout.ActivateOptions();
infoAppender.Layout = infoLayout;
infoAppender.ActivateOptions();
hierarchy.Root.AddAppender(infoAppender);
var errorAppender = new RollingFileAppender
{
Name = "RollingFileAppender_Error",
File = $"Logs/Error/",
Encoding = System.Text.Encoding.UTF8,
AppendToFile = true,
MaxSizeRollBackups = 10,
MaximumFileSize = "20MB",
RollingStyle = RollingFileAppender.RollingMode.Composite,
DatePattern = "yyyy-MM-dd'.log'",
StaticLogFileName = false
};
var errorFilter = new LevelRangeFilter
{
LevelMin = Level.Error,
LevelMax = Level.Fatal
};
errorAppender.AddFilter(errorFilter);
var errorLayout = new PatternLayout
{
ConversionPattern = "[级别:%p]%n[时间:%d]%n[类名:%c]%n[方法:%M]%n[行号:%L]%n[内容:%m]%n[异常:%exception]%n------------------------------------------------------------------------------------------------%n%n"
};
errorLayout.ActivateOptions();
errorAppender.Layout = errorLayout;
errorAppender.ActivateOptions();
hierarchy.Root.AddAppender(errorAppender);
BasicConfigurator.Configure();
private static readonly ILog _logger = LogManager.GetLogger("YOUR CLASS NAME");
_logger.Info("你好");
标签:log4net,Info,infoAppender,RollingFileAppender,new,var,改成,errorAppender,config
From: https://www.cnblogs.com/HandLoong/p/18194380