web项目中使用log4net

  1. AssemblyInfo.cs增加配置:(文件默认相对于当前程序目录-也就是与web.config同目录)
     

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

 

  1. 增加log4net.config配置(也可以合并到web.config)
     

<pre name="code" class="html"><?xmlversion="1.0"?>
<configuration>
  <configSections>
      <section name="log4net"type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/>
  </configSections>
  <log4net debug="true">
    <appendername="RollingLogFileAppender"type="log4net.Appender.RollingFileAppender">
      <filevalue="app_data\\logs\\app.log" />
      <appendToFile value="true"/>
      <maxSizeRollBackupsvalue="10" />
      <maximumFileSize value="2MB"/>
      <rollingStylevalue="Composite" />
      <staticLogFileNamevalue="false" />
      <param name="DatePattern"value=".yyyyMMdd.'txt'" />
      <layouttype="log4net.Layout.PatternLayout">
        <conversionPattern value="%date[%thread] %-5level %logger - %message%newline" />
      </layout>
    </appender>
    <root>
      <level value="ALL" />
      <appender-refref="RollingLogFileAppender" />
    </root>
  </log4net>
</configuration>






 

  1. 使用logger

 

ILog log=LogManager.GetLogger(typeof(MyClass));
log.Error("test ...");


阅读更多
换一批

没有更多推荐了,返回首页