NHibernate的调试技巧和Log4Net配置

1.查看nhibernate写在控制台里的sql语句

在配置文件中有这么个选项,假如把它设置为true,nhibernate会把执行的sql显示在控制台上。

<property name="show_sql">true</property> 

对于控制台应用程序我们可以设置断点后很轻松的看到nhibernate执行了什么sql。

下图是从数据库中读取一条数据。


假如你写了单元测试,从nunit同样可以很轻易地看到。

 

2.配置log4net来查看nhibernate留下的日志

假如你的程序是asp.net程序。那就看不到控制台信息了。那么就使用第二招配置log4net。

按习惯,我还是使用单独的配置文件。当然你也可以配置在应用程序配置文件中(app.config或web.config)。

<?xml version="1.0" encoding="utf-8" ?>
  
<log4net>

    
<appender name="rollingfile" type="log4net.appender.rollingfileappender,log4net" >

      
<param name="file" value="log.txt" />
      
<param name="appendtofile" value="false" />
      
<param name="rollingstyle" value="date" />
      
<param name="datepattern" value="yyyy.mm.dd" />
      
<param name="staticlogfilename" value="true" />

      
<layout type="log4net.layout.patternlayout,log4net">
        
<param name="conversionpattern" value="%d [%t] %-5p %c [%x] &lt;%x{auth}&gt; - %m%n" />
      
</layout>
    
</appender>

    
<root>
      
<!--假如只需要看看sql设置info就够了,假如你要调试可以设置为debug或all-->
      
<priority value="info" />
      
<appender-ref ref="rollingfile" />
    
</root>

  
</log4net>



读取log4net配置的代码

xmlconfigurator.configure(new fileinfo("log4net.cfg.xml"));

运行了程序后你可以在应用程序目录找到log.txt的配置文件。里面记录了包括sql的nhibernate的运行信息。

这是一个日志的片断

2006-08-08 22:22:16,921 [2036] info nhibernate.cfg.environment [(null)] <(null)> - nhibernate 1.0.2
2006-08-08 22:22:16,968 [2036] info nhibernate.cfg.environment [(null)] <(null)> - nhibernate section not found in application configuration file
2006-08-08 22:22:16,968 [2036] info nhibernate.cfg.environment [(null)] <(null)> - using reflection optimizer
2006-08-08 22:22:17,000 [2036] info nhibernate.cfg.configuration [(null)] <(null)> - searching for mapped documents in assembly: ddlly.mydoc.nhibernatetest.log4nettest
2006-08-08 22:22:17,000 [2036] info nhibernate.cfg.configuration [(null)] <(null)> - found mapping documents in assembly: ddlly.mydoc.nhibernatetest.log4nettest.user.hbm.xml
2006-08-08 22:22:17,062 [2036] info nhibernate.dialect.dialect [(null)] <(null)> - using dialect: nhibernate.dialect.mssql2000dialect
2006-08-08 22:22:17,109 [2036] info nhibernate.cfg.binder [(null)] <(null)> - mapping class: ddlly.mydoc.nhibernatetest.log4nettest.user -> users
2006-08-08 22:22:17,156 [2036] info nhibernate.cfg.configuration [(null)] <(null)> - configured sessionfactory: ddlly.mydoc.nhibernatetest.log4nettest
2006-08-08 22:22:17,171 [2036] info nhibernate.cfg.configuration [(null)] <(null)> - processing one-to-many association mappings
2006-08-08 22:22:17,171 [2036] info nhibernate.cfg.configuration [(null)] <(null)> - processing one-to-one association property references
2006-08-08 22:22:17,171 [2036] info nhibernate.cfg.configuration [(null)] <(null)> - processing foreign key constraints
2006-08-08 22:22:17,187 [2036] info nhibernate.dialect.dialect [(null)] <(null)> - using dialect: nhibernate.dialect.mssql2000dialect
2006-08-08 22:22:17,187 [2036] info nhibernate.cfg.settingsfactory [(null)] <(null)> - use outer join fetching: true
2006-08-08 22:22:17,187 [2036] info nhibernate.connection.connectionproviderfactory [(null)] <(null)> - intitializing connection provider: nhibernate.connection.driverconnectionprovider
2006-08-08 22:22:17,187 [2036] info nhibernate.connection.connectionprovider [(null)] <(null)> - configuring connectionprovider
2006-08-08 22:22:17,187 [2036] info nhibernate.cfg.settingsfactory [(null)] <(null)> - optimize cache for minimal puts: false
2006-08-08 22:22:17,203 [2036] info nhibernate.cfg.settingsfactory [(null)] <(null)> - echoing all sql to stdout
2006-08-08 22:22:17,203 [2036] info nhibernate.cfg.settingsfactory [(null)] <(null)> - query language substitutions: {false=0, no='n', yes='y', true=1}
2006-08-08 22:22:17,203 [2036] info nhibernate.cfg.settingsfactory [(null)] <(null)> - cache provider: nhibernate.cache.hashtablecacheprovider
2006-08-08 22:22:17,203 [2036] info nhibernate.cfg.configuration [(null)] <(null)> - instantiating and configuring caches
2006-08-08 22:22:17,218 [2036] info nhibernate.impl.sessionfactoryimpl [(null)] <(null)> - building session factory
2006-08-08 22:22:17,812 [2036] info nhibernate.impl.sessionfactoryobjectfactory [(null)] <(null)> - factory name:ddlly.mydoc.nhibernatetest.log4nettest
2006-08-08 22:22:17,859 [2036] info nhibernate.loader.loader [(null)] <(null)> - select user0_.id as id0_, user0_.email as email0_, user0_.username as username0_, user0_.password as password0_ from users user0_ where user0_.id=@p0


从这个文件我们可以看到nhibernate都做了些什么(包括执行了什么sql,看上面的最后一行)。

当你想更具体的信息可以把priority设置为all,这样可以看到所有信息。

提示:nhibernate会把一般信息记录为info,调试信息记录为debug,错误信息记录为error。

log4net中支持多个appender你可以也把日志记录到数据库等其他地方,请参看log4net的文档,这里不做讲解。

3.让nhibernate的日志不影响你使用log4net写日志

nhibernate总是会调用配置<root>里面的“appender-ref”来写配置。

所以假如你系统本省也使用了log4net记录日志,而不想让nhibernate的日志影响,则可以定义logger。

<?xml version="1.0" encoding="utf-8" ?>
  
<log4net>

    
<root>
      
<!--假如只需要看看sql设置info就够了,假如你要调试可以设置为debug或all-->
      
<priority value="info" />
      
<appender-ref ref="rollingfile" />
    
</root>

    
<logger name="applicationinfolog">
      
<level value="info" />
      
<appender-ref ref="rollingfile1" />
    
</logger>

    
<appender name="rollingfile" type="log4net.appender.rollingfileappender,log4net" >

      
<param name="file" value="log.txt" />
      
<param name="appendtofile" value="false" />
      
<param name="rollingstyle" value="date" />
      
<param name="datepattern" value="yyyy.mm.dd" />
      
<param name="staticlogfilename" value="true" />

      
<layout type="log4net.layout.patternlayout,log4net">
        
<param name="conversionpattern" value="%d [%t] %-5p %c [%x] &lt;%x{auth}&gt; - %m%n" />
      
</layout>
    
</appender>

    
<appender name="rollingfile1" type="log4net.appender.rollingfileappender,log4net" >

      
<param name="file" value="log1.txt" />
      
<param name="appendtofile" value="false" />
      
<param name="rollingstyle" value="date" />
      
<param name="datepattern" value="yyyy.mm.dd" />
      
<param name="staticlogfilename" value="true" />

      
<layout type="log4net.layout.patternlayout,log4net">
        
<param name="conversionpattern" value="%d [%t] %-5p %c [%x] &lt;%x{auth}&gt; - %m%n" />
      
</layout>
    
</appender>

  
</log4net>

日志记录代码

ilog applicationinfolog=applicationinfolog = logmanager.getlogger("applicationinfolog");
applicationinfolog.info(
"记录日志");

此例中nhibernater日志会存在log.txt,系统日志记录在log1.txt。

4.在使用sqlserver时使用事件查看器监视sql

对于sqlserver数据库,假如你没有条件可以完成上面的功能,那么你可以使用事件查看器来监视执行的sql,使用比较简单,不多说了。

原文地址:https://www.cnblogs.com/luluping/p/1758524.html