log4net

程序启动:

#region Log4net注册
        private void RegisterLog4net()
        {
            try
            {
                string configfile = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "log4net.config");
                if (!System.IO.File.Exists(configfile))
                {
                    return;
                }
                log4net.Config.XmlConfigurator.Configure(new System.IO.FileInfo(configfile));
                log = LogManager.GetLogger(this.GetType());
            }
            catch
            {
            }
        } 
        #endregion

 

配置文件:log4net.config

<?xml version="1.0"?>
<configuration>

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

<log4net>
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<param name="File" value="./log/Debug.log"/>
<appendToFile value="true"/>
<rollingStyle value="Composite"/>
<maxSizeRollBackups value="100"/>
<maximumFileSize value="1MB"/>
<staticLogFileName value="false"/>
<param name="DatePattern" value="yyyyMM\\yyyyMMdd\\&quot;Service.log&quot;"/>
<layout type="log4net.Layout.PatternLayout">
<header value="************ System Start **********&#13;&#10;"/>
<footer value="************ System End ************&#13;&#10;"/>
<conversionPattern value="%d %-5p [%c] (line:%L) %n%m%n%n"/>
</layout>
</appender>
<appender name="InfoRollingFileAppender" type="log4net.Appender.RollingFileAppender">
<param name="File" value="./log/Info.log"/>
<appendToFile value="true"/>
<rollingStyle value="Composite"/>
<maxSizeRollBackups value="100"/>
<maximumFileSize value="1MB"/>
<staticLogFileName value="false"/>
<param name="DatePattern" value="yyyyMM\\yyyyMMdd\\&quot;Service.Info.log&quot;"/>
<layout type="log4net.Layout.PatternLayout">
<header value="************ System Start **********&#13;&#10;"/>
<footer value="************ System End ************&#13;&#10;"/>
<conversionPattern value="#%d %-5p [%c] (line:%L) %n%m%n%n"/>
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<param name="LevelMin" value="INFO"/>
<param name="LevelMax" value="INFO"/>
</filter>
</appender>
<appender name="WarnRollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="./log/Warn.log"/>
<appendToFile value="true"/>
<rollingStyle value="Composite"/>
<param name="DatePattern" value="yyyyMM\\yyyyMMdd\\&quot;Service.Warn.log&quot;"/>
<maxSizeRollBackups value="100"/>
<maximumFileSize value="1MB"/>
<staticLogFileName value="false"/>
<layout type="log4net.Layout.PatternLayout">
<header value="************ System Start **********&#13;&#10;"/>
<footer value="************ System End ************&#13;&#10;"/>
<conversionPattern value="!%d %-5p [%c] (line:%L) %m%n"/>
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<param name="LevelMin" value="WARN"/>
<param name="LevelMax" value="WARN"/>
</filter>
</appender>
<appender name="ErrorRollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="./log/Error.log"/>
<appendToFile value="true"/>
<rollingStyle value="Composite"/>
<param name="DatePattern" value="yyyyMM\\yyyyMMdd\\&quot;Service.Error.log&quot;"/>
<maxSizeRollBackups value="100"/>
<maximumFileSize value="1MB"/>
<staticLogFileName value="false"/>
<layout type="log4net.Layout.PatternLayout">
<header value="************ System Start **********&#13;&#10;"/>
<footer value="************ System End ************&#13;&#10;"/>
<conversionPattern value="x!!%d %-5p [%c] (line:%L) %m%n"/>
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<param name="LevelMin" value="ERROR"/>
<param name="LevelMax" value="FATAL"/>
</filter>
</appender>
<root>
<level value="ALL"/>
<appender-ref ref="RollingFileAppender"/>
<appender-ref ref="InfoRollingFileAppender"/>
<appender-ref ref="WarnRollingFileAppender"/>
<appender-ref ref="ErrorRollingFileAppender"/>
</root>
</log4net>
</configuration>

log4net,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。