首页 > 代码库 > log4net的分类型输出文件的配置

log4net的分类型输出文件的配置

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
    </configSections>

    <log4net>

        <root>
            <level value="ALL" />
            <appender-ref ref="InfoAppender" />
            <appender-ref ref="WarnAppender" />
            <appender-ref ref="ErrorAppender" />
            <appender-ref ref="FatalAppender" />
            <appender-ref ref="DebugAppender" />
        </root>
<!-- 输出模式定义 --> <appender name="InfoAppender" type="log4net.Appender.RollingFileAppender"> <!--日志路径: 兼容相对路径和绝对路径,此语句等价于<File value="http://www.mamicode.com/Logs/"/> --> <param name= "File" value= "Logs\"/> <!--是否是向文件中追加日志--> <param name= "AppendToFile" value= "true"/> <!--保留天数--> <param name= "MaxSizeRollBackups" value= "-1"/> <!--日志文件名是否固定不变的--> <param name= "StaticLogFileName" value= "false"/> <!--日志文件名格式为:2017-01-12_Info.log--> <param name= "DatePattern" value= "yyyy-MM-dd&quot;_Info.log&quot;"/> <!--日志根据日期滚动--> <param name= "RollingStyle" value= "Date"/> <!-- 写入记录文件时的格式%p应该指模式如WARN,%m应该指Message值,%n应该是换行的意思。接着输出Exception --> <layout type="log4net.Layout.PatternLayout"> <param name="ConversionPattern" value="%date{yyyy-MM-dd HH:mm:ss.fff} %-8p %m%n" /> </layout> <!-- 此模式输出的级别范围 -->
       <!-- FATAL > ERROR > WARN > INFO > DEBUG -->  <filter type="log4net.Filter.LevelRangeFilter"> <levelMin value="INFO" /> <levelMax value="INFO" /> </filter> </appender> <appender name="WarnAppender" type="log4net.Appender.RollingFileAppender"> <File value= "Logs\"/> <param name= "AppendToFile" value= "true"/> <param name= "MaxSizeRollBackups" value= "10"/> <!-- 能保存的文件最大值,假设某次输出后该文件如mm_warn.log的大小将会超过最大值3KB,则此次输出仍有效,但是下次输出则会将该文件重命名为mm_warn.log.1,且生成一个新的mm_warn.log文件以供记录输出内容。 --> <param name="MaximumFileSize" value="3KB" /> <param name= "StaticLogFileName" value= "false"/> <param name= "DatePattern" value= "yyyy-MM-dd&quot;_Warn.log&quot;"/> <param name= "RollingStyle" value= "Composite"/> <!-- 写入记录文件时的格式 --> <layout type="log4net.Layout.PatternLayout"> <param name="ConversionPattern" value="%date{yyyy-MM-dd HH:mm:ss.fff} %-8p %m%n" /> </layout> <!-- 此模式输出的级别范围 --> <filter type="log4net.Filter.LevelRangeFilter"> <levelMin value="WARN" /> <levelMax value="WARN" /> </filter> </appender> <appender name="ErrorAppender" type="log4net.Appender.RollingFileAppender"> <param name= "File" value= "Logs\"/> <param name= "AppendToFile" value= "true"/> <param name= "MaxSizeRollBackups" value= "10"/> <!-- 能保存的文件最大值 --> <param name="MaximumFileSize" value="1MB" /> <param name= "StaticLogFileName" value= "false"/> <param name= "DatePattern" value= "yyyy-MM-dd&quot;_Error.log&quot;"/> <param name= "RollingStyle" value= "Date"/> <layout type="log4net.Layout.PatternLayout"> <param name="ConversionPattern" value="%date{yyyy-MM-dd HH:mm:ss.fff} %-8p %m%n" /> </layout> <!-- 此模式输出的级别范围 --> <filter type="log4net.Filter.LevelRangeFilter"> <levelMin value="ERROR" /> <levelMax value="ERROR" /> </filter> </appender> <appender name="FatalAppender" type="log4net.Appender.RollingFileAppender"> <param name= "File" value= "Logs\"/> <param name= "AppendToFile" value= "true"/> <param name= "MaxSizeRollBackups" value= "10"/> <param name= "StaticLogFileName" value= "false"/> <param name= "DatePattern" value= "yyyy-MM-dd&quot;_Fatal.log&quot;"/> <param name= "RollingStyle" value= "Date"/> <layout type="log4net.Layout.PatternLayout"> <param name="ConversionPattern" value="%date{yyyy-MM-dd HH:mm:ss.fff} %-8p %m%n" /> </layout> <!-- 此模式输出的级别范围 --> <filter type="log4net.Filter.LevelRangeFilter"> <levelMin value="FATAL" /> <levelMax value="FATAL" /> </filter> </appender> <appender name="DebugAppender" type="log4net.Appender.RollingFileAppender"> <param name= "File" value= "Logs\"/> <param name= "AppendToFile" value= "true"/> <param name= "MaxSizeRollBackups" value= "10"/> <param name= "StaticLogFileName" value= "false"/> <param name= "DatePattern" value= "yyyy-MM-dd&quot;_Debug.log&quot;"/> <param name= "RollingStyle" value= "Date"/> <layout type="log4net.Layout.PatternLayout"> <param name="ConversionPattern" value="%date{yyyy-MM-dd HH:mm:ss.fff} %-8p %m%n" /> </layout> <!-- 此模式输出的级别范围 --> <filter type="log4net.Filter.LevelRangeFilter"> <levelMin value="DEBUG" /> <levelMax value="DEBUG" /> </filter> </appender> </log4net> </configuration>

 

log4net的分类型输出文件的配置