IT干货网

log4net配置详细说明

xmjava 2022年03月15日 编程设计 145 0

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);


评论关闭
IT干货网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!

选择HttpHandler还是HttpModule