用Log4Net记录NHibernate中执行的SQL语句及执行时间

首页,在web.config中加入以下配置:

<configuration>
   <configSections>
     <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
   </configSections>
   <!--log4net 配置-->
   <log4net debug="true">
     <appender name="rollingFile" type="log4net.Appender.RollingFileAppender,log4net">
       <param name="File" value="log/log.txt"/>
       <param name="AppendToFile" value="false"/>
       <param name="RollingStyle" value="Date"/>
       <param name="StaticLogFileName" value="true"/>
       <layout type="log4net.Layout.PatternLayout">
         <param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />
       </layout>
     </appender>
     
     <logger name="NHibernate" additivity="true">
       <level value="ERROR"/>
       <appender-ref ref="DebugAppender" />
     </logger>
     <logger name="NHibernate.SQL" additivity="true">
       <level value="DEBUG"/>
       <appender-ref ref="DebugAppender" />
     </logger>
     
     <logger name="NHibernate.AdoNet.AbstractBatcher" additivity="true">
       <level value="DEBUG"/>
       <appender-ref ref="DebugAppender" />
     </logger>
     
     <root>
       <!-- levels: DEBUG, INFO, WARN, ERROR, FATAL -->
       <level value="DEBUG"/>
       <appender-ref ref="rollingFile"/>
     </root>
   </log4net>
 </configuration>

然后,在Global.asax加入以下代码并添加相应引用:

 1 private static log4net.ILog logger = log4net.LogManager.GetLogger("Logger");
 2 
 3 private static readonly ILog applicationInfoLog = LogManager.GetLogger("ApplicationInfoLog");
 4 
 5 protected override void Application_Start(object sender, EventArgs e)
 6 {
 7 //初始化log4net
 8 log4net.Config.XmlConfigurator.Configure();
 9 
10 base.Application_Start(sender, e);
11 
12 }

 

 

posted @ 2014-01-08 19:54  奋斗中...  阅读(2774)  评论(0编辑  收藏  举报