首页 > 代码库 > log4net配置要点

log4net配置要点

1。log4net.config. 这个是最重要的配置,不用多说

 

<?xml version="1.0" encoding="utf-8" ?>
<log4net>
  <!-- Define some output appenders -->
  <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
    <file value=".\log\log.txt" />
    <appendToFile value="true" />
    <!--可以为:Once|Size|Date|Composite-->
    <!--Composite为Size和Date的组合-->
    <rollingStyle value="Composite" />
    <!--日志最大个数,都是最新的-->
    <!--rollingStyle节点为Date时,该节点不起作用-->
    <!--rollingStyle节点为Size时,只能有value个日志-->
    <!--rollingStyle节点为Composite时,每天有value个日志-->
    <maxSizeRollBackups value="-1" />
    <!--当备份文件时,为文件名加的后缀-->
    <datePattern value=".yyyy-MM-dd" />
    <!--日志文件大小,可用的单位:KB|MB|GB-->
    <maximumFileSize value="10MB" />
    <layout type="log4net.Layout.PatternLayout">
      <header value="[Header]&#13;&#10;" />
      <footer value="[Footer]&#13;&#10;" />
      <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
    </layout>
    <!--输出级别-->
    <filter type="log4net.Filter.LevelRangeFilter">
      <levelMax value="WARN" />
    </filter>
  </appender>
  <appender name="RollingErrorLogFileAppender" type="log4net.Appender.RollingFileAppender">
    <file value=".\log\log_error.txt" />
    <appendToFile value="true" />
    <!--可以为:Once|Size|Date|Composite-->
    <!--Composite为Size和Date的组合-->
    <rollingStyle value="Composite" />
    <!--日志最大个数,都是最新的-->
    <!--rollingStyle节点为Date时,该节点不起作用-->
    <!--rollingStyle节点为Size时,只能有value个日志-->
    <!--rollingStyle节点为Composite时,每天有value个日志-->
    <maxSizeRollBackups value="-1" />
    <!--当备份文件时,为文件名加的后缀-->
    <datePattern value=".yyyy-MM-dd" />
    <!--日志文件大小,可用的单位:KB|MB|GB-->
    <maximumFileSize value="10MB" />
    <layout type="log4net.Layout.PatternLayout">
      <header value="[Header]&#13;&#10;" />
      <footer value="[Footer]&#13;&#10;" />
      <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
    </layout>
    <!--输出级别-->
    <filter type="log4net.Filter.LevelRangeFilter">
      <levelMin value="ERROR" />
    </filter>
    <!--必须结合起来用,第一个只过滤出WARN,第二个拒绝其它其它日志输出-->
    <!--<filter type="log4net.Filter.LevelMatchFilter">
            <param name="LevelToMatch" value="http://www.mamicode.com/ERROR" />
        </filter>
        <filter type="log4net.Filter.DenyAllFilter" />-->
  </appender>
  <root>
    <level value="INFO" />
  </root>
  <logger name="WebLogger">
    <level value="WARN" />
    <appender-ref ref="RollingLogFileAppender" />
    <appender-ref ref="RollingErrorLogFileAppender" />
  </logger>
  <logger name="ServiceLogger">
    <level value="INFO" />
    <appender-ref ref="RollingLogFileAppender" />
    <appender-ref ref="RollingErrorLogFileAppender" />
    <appender-ref ref="ColoredConsoleAppender" />
  </logger>
</log4net>

 

<configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=4.4.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
  </configSections>
  <log4net configSource="config\log4net.config" />

 

2. global.asax.cn 中要注册下,才能访问。log4net.Config.XmlConfigurator.Configure();

 

3。 assemblyinfo.cs 中,[assembly: log4net.Config.XmlConfigurator(ConfigFile = "config/log4net.config", Watch = true)]  可以不用配置

log4net配置要点