码迷,mamicode.com
首页 > Web开发 > 详细

log4net日志分割,按大小分割

时间:2015-07-04 11:00:55      阅读:155      评论:0      收藏:0      [点我收藏+]

标签:

最近写了一个socket通信的手表在线服务端,在日志方面,记录下Log4net日志分割

1.引入log4net.dll

2.web.config添加configsection handler 映射:

<configuration>
    <configSections>
      <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
    </configSections>
</configuration>

3.建立 log4net.config 文件,此文件在logs文件夹下面按日期生成

<?xml version="1.0" encoding="utf-8"?>
<log4net>
  <!-- OFF, FATAL, ERROR, WARN, INFO, DEBUG, ALL -->
  <!-- Set root logger level to ERROR and its appenders -->
  <root>
    <level value="ALL"/>
    <appender-ref ref="SysAppender"/>
  </root>

  <!-- Print only messages of level DEBUG or above in the packages -->
  <logger name="WebLogger">
    <level value="DEBUG"/>
  </logger>
  
  <appender name="SysAppender" type="log4net.Appender.RollingFileAppender,log4net" >
    <param name="File" value="Logs/" />
    <param name="AppendToFile" value="true" />
    <param name="RollingStyle" value="Date" />
    <param name="DatePattern" value="&quot;Logs_&quot;yyyyMMdd&quot;.txt&quot;" />
    <param name="StaticLogFileName" value="false" />
    <layout type="log4net.Layout.PatternLayout,log4net">
      <param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />
      <param name="Header" value="&#13;&#10;----------------------header--------------------------&#13;&#10;" />
      <param name="Footer" value="&#13;&#10;----------------------footer--------------------------&#13;&#10;" />
    </layout>
  </appender>
  <appender name="consoleApp" type="log4net.Appender.ConsoleAppender,log4net">
    <layout type="log4net.Layout.PatternLayout,log4net">
      <param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />
    </layout>
  </appender>
</log4net>

 

4.按大小分割的映射文件

<?xml version="1.0" encoding="utf-8"?>
<log4net>
  <!-- OFF, FATAL, ERROR, WARN, INFO, DEBUG, ALL -->
  <!-- Set root logger level to ERROR and its appenders -->
  <root>
    <level value="ALL"/>
    <appender-ref ref="SysAppender"/>
  </root>

  <!-- Print only messages of level DEBUG or above in the packages -->
  <logger name="WebLogger">
    <level value="DEBUG"/>
  </logger>
  
  <appender name="SysAppender" type="log4net.Appender.RollingFileAppender">
    <param name="File" value="Logs\Logs.txt"/>
    <param name="AppendToFile" value="true"/>
    <param name="MaxSizeRollBackups" value="12"/> <!-- 切割最多文件数 -->
    <param name="MaximumFileSize" value="500KB"/> <!-- 每个文件的大小 -->
    <param name="RollingStyle" value="Size"/>
    <param name="StaticLogFileName" value="true"/>
    <layout type="log4net.Layout.PatternLayout">
      <param name="ConversionPattern" value="%d [%t] %-5p %c [%x] - %m%n"/>
    </layout>
  </appender>
</log4net>

我的注解:

配置文件是用反射和名称生成对应的类的声明, RollingFileAppender  SysAppender 对象,下面是这个类的各个参数。这个类一般是这样设计的

log4net日志分割,按大小分割

标签:

原文地址:http://www.cnblogs.com/hualiu0/p/4620198.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!