Log4Net如何将日志按不同类型写入多个文件中

建了3个日志文件

事务日志:log.txt
错误日志:log_error.txt  
操作日志:log_event.txt

分别把日志信息写到这 3个文件中:

<log4net>

    <root>

      <!--

            <level value="ALL" />

            <appender-ref ref="rootFile" />

            -->

    </root>

    <logger name="Livan's Logger">

      <level value="ALL" />

      <appender-ref ref="Loging" />

      <appender-ref ref="EventLoging"/>

      <appender-ref ref="ErrorLoging" />

    </logger>

    <!--信息日志-->

    <appender name="Loging" type="log4net.Appender.RollingFileAppender,log4net" >

      <param name="File" value="Log\log.txt" />

      <param name="AppendToFile" value="true" />

      <param name="RollingStyle" value="Date" />

      <param name="DatePattern" value="yyyyMMdd" />

      <param name="StaticLogFileName" value="true" />

      <layout type="log4net.Layout.PatternLayout,log4net">

        <param name="ConversionPattern" value="%d [%t] %-5p %c [%x] - %m%n"  />

        <param name="Header" value="
" />

      </layout>

      <filter type="log4net.Filter.LevelRangeFilter">

        <param name="LevelMin" value="INFO" />

        <param name="LevelMax" value="INFO" />

      </filter>

    </appender>

    <!--警告,注意,通知日志-->

    <appender name="EventLoging" type="log4net.Appender.RollingFileAppender,log4net" >

      <param name="File" value="Log\log_event.txt" />

      <param name="AppendToFile" value="true" />

      <param name="RollingStyle" value="Date" />

      <param name="DatePattern" value="yyyyMMdd" />

      <param name="StaticLogFileName" value="true" />

      <layout type="log4net.Layout.PatternLayout,log4net">

        <param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss}%newline%message%n" />

        <param name="Header" value="
[Header]
" />

        <param name="Footer" value="[Footer]
" />

      </layout>

      <filter type="log4net.Filter.LevelRangeFilter">

        <param name="LevelMin" value="WARN" />

        <param name="LevelMax" value="WARN" />

      </filter>

    </appender>

    <!--错误日志-->

    <appender name="ErrorLoging" type="log4net.Appender.RollingFileAppender,log4net" >

      <param name="File" value="Log\log_error.txt" />

      <param name="AppendToFile" value="true" />

      <param name="RollingStyle" value="Date" />

      <param name="DatePattern" value="yyyyMMdd" />

      <param name="StaticLogFileName" value="true" />

      <layout type="log4net.Layout.PatternLayout,log4net">

        <param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss}%newline%message%n" />

        <param name="Header" value="
[Header]
" />

        <param name="Footer" value="[Footer]
" />

      </layout>

      <filter type="log4net.Filter.LevelRangeFilter">

        <param name="LevelMin" value="ERROR" />

        <param name="LevelMax" value="ERROR" />

      </filter>

    </appender>

  </log4net>



后台调用


log4net.ILog log = log4net.LogManager.GetLogger("Livan's Logger");

log.Info(message);

log.Warn(message);

...




程序会分别写入不同txt文件中

  

 

posted @ 2015-05-25 10:35  伟大的厨师  阅读(637)  评论(0编辑  收藏  举报