- 引用log4net.dll
- AssemblyInfo.cs文件添加:[assembly: log4net.Config.XmlConfigurator(ConfigFile = "configs/log4net.config", Watch = true)](log4net配置文件单独放置)
- Global全局文件中添加:log4net.Config.XmlConfigurator.Configure();
- web.config中添加log4net节点
-
<configSections>
<section name="log4net" allowLocation="true" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
-
- log4net.config文件(例如:)
<?xml version="1.0" encoding="utf-8"?> <configuration> <configSections> <section name="log4net" allowLocation="true" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" /> </configSections> <log4net> <root> <level value="DEBUG" /> <appender-ref ref="RollingLogFileAppender" /> </root> <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender"> <param name="File" value="log/log-"/> <param name="AppendToFile" value="true"/> <param name="MaxSizeRollBackups" value="10"/> <param name="MaximumFileSize" value="10MB"/> <param name="StaticLogFileName" value="false"/> <param name="DatePattern" value="yyyyMMdd".log""/> <param name="RollingStyle" value="Date"/> <layout type="log4net.Layout.PatternLayout"> <param name="ConversionPattern" value="%d [%t] %-5p %c [%x] - %m%n"/> </layout> </appender> </log4net> </configuration>