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. MyBatis3.4.0以上的分页插件错误:Could not find method on interface org.apache.ibatis.executor.statement.StatementHandler named prepare. Cause: java.lang.NoSuchMethodException: org.apache.ibatis.executor.stateme

    错误: Could not find method on interface org.apache.ibatis.executor.statement.StatementHandler named p ...

  2. Codeforces 801C Voltage Keepsake(二分枚举+浮点(模板))

    题目链接:http://codeforces.com/contest/801/problem/C 题目大意:给你一些电器以及他们的功率,还有一个功率一定的充电器可以给这些电器中的任意一个充电,并且不计 ...

  3. sql server 约束 查找

    --1.主键约束 SELECT tab.name AS [表名], idx.name AS [主键名称], col.name AS [主键列名] FROM sys.indexes idx JOIN s ...

  4. 从一道简单的dp题中学到的...

    今天想学点动态规划的知识,于是就看了杭电的课件,数塔问题啊,LCS啊都是比较经典的动规了,然后随便看了看就开始做课后练习题... HDOJ 1421 搬寝室 http://acm.hdu.edu.cn ...

  5. oracle查看所有表及各表行数

    https://zhidao.baidu.com/question/131972827.html

  6. mycat性能调优

    http://blog.csdn.net/wangshuang1631/article/details/69056070

  7. CSU - 2059 Water Problem

    Description ​ 一条'Z'形线可以将平面分为两个区域,那么由N条Z形线所定义的区域的最大个数是多少呢?每条Z形线由两条平行的无限半直线和一条直线段组成 Input 首先输入一个数字T(T& ...

  8. leetcode 202. 快乐数 python实现

    思想: 对输入数据 把每个位数平方求和 得到结果如果是1 就返回真 否则 对这个结果递归 啥时候事后返回假: 返回假 说明进入无限循环了. 啥时候会无限循环? 某一次求平方和的结果,之前得到过这个结果 ...

  9. CentOS系统php5.6安装ImageMagick处理webp格式图片

    1.先安装webp yum install libwebp 2.编译安装ImageMagick 之前有过yum安装的先卸载 yum remove ImageMagick 我使用的是老版本ImageMa ...

  10. luoguP3871 [TJOI2010]中位数

    题目链接 luoguP3871 [TJOI2010]中位数 题解 平衡树 代码 #include<vector> #include<cstdio> #include<cs ...