如题,解决如下:
1、log4net.config配置如下:
<?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" /> </configSections> <appSettings> </appSettings> <log4net> <root> <level value="ALL"/> <appender-ref ref="InfoAppender"/> <appender-ref ref="ErrorAppender" /> </root> <logger name="logerror"> <level value="ALL"/> <level value="ERROR" /> <appender-ref ref="ErrorAppender" /> </logger> <logger name="loginfo"> <level value="ALL"/> <level value="INFO" /> <appender-ref ref="InfoAppender" /> </logger> <appender name="ErrorAppender" type="log4net.Appender.RollingFileAppender,log4net"> <!--日志路径--> <param name="File" value="Log\" /> <!--是否是向文件中追加日志--> <param name="AppendToFile" value="true" /> <!--log保留天数--> <param name="MaxSizeRollBackups" value="100" /> <param name="MaxFileSize" value="10240" /> <!--日志文件名是否是固定不变的--> <param name="StaticLogFileName" value="false" /> <!--日志文件名格式为:2008-08-31.log--> <param name="DatePattern" value="yyyyMMdd'.log'" /> <!--日志根据日期滚动--> <param name="RollingStyle" value="Date" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline"/> </layout> </appender> <appender name="InfoAppender" type="log4net.Appender.RollingFileAppender,log4net"> <param name="File" value="Log\" /> <param name="AppendToFile" value="true" /> <param name="MaxFileSize" value="10240" /> <param name="MaxSizeRollBackups" value="100" /> <param name="StaticLogFileName" value="false" /> <param name="DatePattern" value="yyyyMMdd'.log'" /> <param name="RollingStyle" value="Date" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline"/> </layout> </appender> </log4net> <startup> <supportedRuntime version="v2.0.50727"/> </startup> </configuration>
2、在网站web.config中加入一句话,位置如下:
<configuration>
<configSections>
<section name="log4net"
type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
</configSections>
</configuration>
3、引用类文件中加入[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", ConfigFileExtension = "log4net", Watch = true)]