log4net配置详细说明

1.web网站中创建log4net.config

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
  </configSections>
  <log4net>
    <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
      <file value="log/log.txt" />
      <appendToFile value="true" />
      <rollingStyle value="Size" />
      <maxSizeRollBackups value="1000" />
      <maximumFileSize value="1024KB" />
      <staticLogFileName value="true" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
      </layout>
      <filter type="log4net.Filter.LevelMatchFilter">
        <param name="LevelToMatch" value="INFO" />
      </filter>
    </appender>
    <appender name="RollingErrorFileAppender" type="log4net.Appender.RollingFileAppender">
      <file value="log/error_log.txt" />
      <appendToFile value="true" />
      <rollingStyle value="Size" />
      <maxSizeRollBackups value="1000" />
      <maximumFileSize value="1024KB" />
      <staticLogFileName value="true" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
      </layout>
      <filter type="log4net.Filter.LevelRangeFilter">
        <levelMin value="WARN" />
        <levelMax value="FATAL" />
      </filter>
    </appender>
    <root>
      <level value="INFO" />
      <appender-ref ref="RollingFileAppender" />
    </root>
    <logger name="AccountInfoLogger">
      <appender-ref ref="RollingFileAppender"/>
      <appender-ref ref="RollingErrorFileAppender"/>
      <level value="INFO"/>
    </logger>
  </log4net>
 
    <system.web>
      <compilation debug="true" targetFramework="4.0" />
    </system.web>

</configuration>
2.调用日志的类库或web的AssemblyInfo.cs中添加组件

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

3.调用

logger.Error("", ex);

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。