log4net使用
1添加log4net.config配置文件
<?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" /> </configSections> <!--Log4net Begin by Tony 2008.11.20--> <log4net> <!--定义一个日志输出目的地--> <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender"> <file value="G:\\website\\www.xftka.com\\logs\\" /> <appendToFile value="true" /> <!--按照日期进行变换日志文件--> <rollingStyle value="Date" /> <datePattern value="yyyyMMdd'.txt'"/> <!--<lockingModel type="log4net.Appender.RollingFileAppender+MinimalLock" />--> <!--最小锁定模型以允许多个进程可以写入同一个文件--> <lockingModel type="log4net.Appender.FileAppender+MinimalLock" /> <!--保留的log文件数量 超过此数量后 自动删除之前的 --> <maxSizeRollBackups value="100" /> <staticLogFileName value="false" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" /> </layout> </appender> <!--输出到数据库--> <appender name="AdoNetAppender_SqlServer" type="log4net.Appender.AdoNetAppender"> <connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/> <connectionString value="Data Source=.\sqlexpress;initial catalog=xft-shop;User ID=sa;Password=1" providerName="System.Data.SqlClient"/> <commandText value="INSERT INTO xft_log_card ([Date],[Thread],[Level],[Logger],[Message]) VALUES (@log_date, @thread, @log_level, @logger, @message)"/> <parameter> <parameterName value="@log_date"/> <dbType value="DateTime"/> <layout type="log4net.Layout.PatternLayout" value="%date{yyyy'-'MM'-'dd HH':'mm':'ss'.'fff}"/> </parameter> <parameter> <parameterName value="@thread"/> <dbType value="String"/> <size value="255"/> <layout type="log4net.Layout.PatternLayout" value="%thread"/> </parameter> <parameter> <parameterName value="@log_level"/> <dbType value="String"/> <size value="50"/> <layout type="log4net.Layout.PatternLayout" value="%level"/> </parameter> <parameter> <parameterName value="@logger"/> <dbType value="String"/> <size value="255"/> <layout type="log4net.Layout.PatternLayout" value="%logger"/> </parameter> <parameter> <parameterName value="@message"/> <dbType value="String"/> <size value="4000"/> <layout type="log4net.Layout.PatternLayout" value="%message"/> </parameter> </appender> <root> <!--指定输出日志的等级--> <level value="ALL"/> <appender-ref ref="RollingFileAppender" /> <appender-ref ref="RollingLogFileAppender" /> <appender-ref ref="ConsoleAppender" /> <appender-ref ref="AdoNetAppender_SqlServer" /> </root> </log4net> <!--Log4net End--> </configuration>
2 Global.asax.cs里
1 readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); 2 protected void Application_Start() 3 { 4 System.IO.FileInfo fileinfo = new System.IO.FileInfo(Server.MapPath("~/log4net.config")); 5 log4net.Config.XmlConfigurator.Configure(fileinfo); 6 7 log.Info("网站启动......"); 8 }
可参考实例代码