1、配置文件:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<log4net>
<logger name="Weixin" additivity="false">
<level value="All" />
<appender-ref ref="AccountRollingFileAppender" />
</logger>
<appender name="AccountRollingFileAppender" type="log4net.Appender.RollingFileAppender">
<param name="File" value="LogsWeixin.log" />
<param name="AppendToFile" value="true" />
<param name="RollingStyle" value="Size" />
<param name="CountDirection" value="1" />
<param name="MaximumFileSize" value="1MB" />
<param name="MaxSizeRollBackups" value="1024" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />
</layout>
</appender>
</log4net>
</configuration>
2、初始化:
string logConfigPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Log4Net.config");
XmlConfigurator.Configure(new FileStream(logConfigPath, FileMode.Open));