先在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="../WebServiceErrLog.txt" /> <param name="datePattern" value="yyyy-MM-dd HH:mm" /> <param name="AppendToFile" value="true" /> <layout type="log4net.Layout.PatternLayout"> <param name="ConversionPattern" value="%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="%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="%d [%t] %-5p %c [%x] - %m%n" /> </layout> </appender> <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender"> <param name="File" value="C:\Log.txt" /> <param name="AppendToFile" value="true" /> <param name="MaxSizeRollBackups" value="10" /> <param name="MaximumFileSize" value="5MB" /> <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> <root> <level value="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 <记录到记录日志到文件,可以设置文件的名字,或者多个文件>
评论