首页 > 代码库 > Log4Net

Log4Net

下载 http://logging.apache.org/log4net/先在web.config中的<configuration>节点下添加如下配置:<configSections>  <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" /></configSections><configuration>节点下添加如下log4net节点配置:<log4net debug="false">    <appender name="LogFileAppender" type="log4net.Appender.FileAppender" >        <param name="File" value=http://www.mamicode.com/"../WebServiceErrLog.txt" />        <param name="datePattern" value=http://www.mamicode.com/"yyyy-MM-dd HH:mm" />        <param name="AppendToFile" value=http://www.mamicode.com/"true" />        <layout type="log4net.Layout.PatternLayout">            <param name="ConversionPattern" value=http://www.mamicode.com/"%d [%t] %-5p %c [%x] - %m%n" />        </layout>    </appender>    <appender name="HttpTraceAppender" type="log4net.Appender.ASPNetTraceAppender" >        <layout type="log4net.Layout.PatternLayout">            <param name="ConversionPattern" value=http://www.mamicode.com/"%d [%t] %-5p %c [%x] - %m%n" />        </layout>    </appender>    <appender name="EventLogAppender" type="log4net.Appender.EventLogAppender" >        <layout type="log4net.Layout.PatternLayout">            <param name="ConversionPattern" value=http://www.mamicode.com/"%d [%t] %-5p %c [%x] - %m%n" />        </layout>    </appender>    <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">        <param name="File" value=http://www.mamicode.com/"C:\Log.txt" />        <param name="AppendToFile" value=http://www.mamicode.com/"true" />        <param name="MaxSizeRollBackups" value=http://www.mamicode.com/"10" />        <param name="MaximumFileSize" value=http://www.mamicode.com/"5MB" />        <param name="RollingStyle" value=http://www.mamicode.com/"Size" />        <param name="StaticLogFileName" value=http://www.mamicode.com/"true" />        <layout type="log4net.Layout.PatternLayout">            <param name="ConversionPattern" value=http://www.mamicode.com/"%d [%t] %-5p %c [%x] - %m%n" />        </layout>    </appender>    <root>        <level value=http://www.mamicode.com/"DEBUG" />        <appender-ref ref="LogFileAppender" />    </root></log4net>在Global.asax.cs的Application_Start过程中添加如下代码:log4net.Config.DOMConfigurator.Configure();调用:log4net.ILog logger = log4net.LogManager.GetLogger(this.GetType());logger.Info(info);附加器:log4net.Appender.AdoNetAppender <记录到数据库配置access,sqlserver或者其他的数据库>log4net.Appender.FileAppender <记录到记录日志到单个文件>log4net.Appender.EventLogAppender <记录到记录日志到操作系统的事件>log4net.Appender.RollingFileAppender <记录到记录日志到文件,可以设置文件的名字,或者多个文件>