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,使用比较简单,不多说了。

NHibernate的调试技巧和Log4Net配置的更多相关文章

  1. Visual Studio高级调试技巧

    1. 设置软件断点,运行到目标位置启动调试器 方法①:使用汇编指令(注:x64 c++不支持嵌入汇编) _asm 方法②:编译器提供的方法 __debugbreak(); 方法③:使用windows ...

  2. 【转】你所不知道的Android Studio调试技巧

    这篇写Android studio debug技巧个人觉得写得不错,转自:http://www.jianshu.com/p/011eb88f4e0d# Android Studio目前已经成为开发An ...

  3. iOS各种调试技巧豪华套餐

    转载自http://www.cnblogs.com/daiweilai/p/4421340.html 目录 前言 逼优鸡 知己知彼 百战不殆 抽刀断Bug 普通操作 全局断点(Global Break ...

  4. 你所不知道的Android Studio调试技巧

    转载:http://www.jianshu.com/p/011eb88f4e0d Android Studio目前已经成为开发Android的主要工具,用熟了可谓相当顺手.作为开发者,调试并发现bug ...

  5. Visual Studio原生开发的20条调试技巧(下)

    我的上篇文章<Vistual Studio原生开发的10个调试技巧>引发了很多人的兴趣,所以我决定跟大家分享更多的调试技巧.接下来你又能看到一些对于原生应用程序的很有帮助的调试技巧(接着上 ...

  6. Visual Studio原生开发的10个调试技巧

    这篇文章只介绍了一些有关Visual Studio的基本调试技巧,但是还有其他一些同样有用的技巧.我整理了一些Visual Studio(至少在VS 2008下)原生开发的调试技巧.(如果你是工作在托 ...

  7. xcode的调试技巧

    转自:http://www.cnblogs.com/daiweilai/p/4421340.html#biyouji 目录 前言逼优鸡知己知彼 百战不殆抽刀断Bug 普通操作 全局断点(Global ...

  8. LTE Module User Documentation(翻译15)——示例程序、参考场景以及故障检测和调试技巧

    LTE用户文档 (如有不当的地方,欢迎指正!)     21 Examples Programs(示例程序)   路径 src/lte/examples/ 包含一些示例仿真程序,这些例子表明如何仿真不 ...

  9. 调试技巧--Windows端口号是否被占用

    调试技巧--Windows端口号是否被占用 一.端口概念 10.0.0.0~10.255.255.255,172.16.0.0~172.16.255.255, 192.168.0.0~192.168. ...

随机推荐

  1. Ad Hoc Distributed Queries的启用与关闭

    启用Ad Hoc Distributed Queries: exec sp_configure 'show advanced options',1 reconfigure exec sp_config ...

  2. linux c获取本地时间

    在标准C/C++中,我们可通过tm结构来获得日期和时间,tm结构在time.h中的定义如下: #ifndef _TM_DEFINED struct tm { int tm_sec; /* 秒–取值区间 ...

  3. getch与getchar区别

    getch(): 所在头文件:conio.h 函数用途:从控制台读取一个字符,但不显示在屏幕上 getchar(): 所在头文件:stdio.h getch与getchar基本功能相同,差别是getc ...

  4. 使用Github的gh-pages分支展示一个页面

    Github有一个Github pages的功能可以搭建博客或者托管网页,是免费使用的. 首先你的注册Github账号 下载安装git Github官网操作 登录到Github上,创建一个名为 Git ...

  5. MyBatis的动态插入语句(经常报‘无效的列类型’)

    最近在工作中经常遇到一个情况:通过mybatis的标签执行插入语句,当表中字段比较多的时候,需要全部插入,而有时候的需求是只插入其中几个字段,但是会报错. 原来的语句,必须把所有字段都Set值. &l ...

  6. Dos.ORM - 目录、介绍

    引言: Dos.ORM(原Hxj.Data)于2009年发布.2015年正式开源,该组件已在数百个成熟项目中应用,是目前国内用户量最大.最活跃.最完善的国产ORM.初期开发过程中参考了NBear与My ...

  7. JavaScript工程师都应懂的33个概念

    最近福利发的有点多啊,各种硬干货,小伙伴们是不是觉得很爽啊.Github真的蕴含着各种各样的宝藏,难怪各个大厂也都纷纷贡献自己的代码到Github上. 所以各种干货还是会源源不断的po给大家,觉得有帮 ...

  8. easyui tree tabs

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  9. firewalld启动问题

    问题 在查看或启动firewalld服务时,提示"Warning: firewalld.service changed on disk. Run 'systemctl daemon-relo ...

  10. Mybatis 源码分析之事物管理

    Mybatis 提供了事物的顶层接口: public interface Transaction { /** * Retrieve inner database connection * @retur ...