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] <%x{auth}> - %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] <%x{auth}> - %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] <%x{auth}> - %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配置的更多相关文章
- Visual Studio高级调试技巧
1. 设置软件断点,运行到目标位置启动调试器 方法①:使用汇编指令(注:x64 c++不支持嵌入汇编) _asm 方法②:编译器提供的方法 __debugbreak(); 方法③:使用windows ...
- 【转】你所不知道的Android Studio调试技巧
这篇写Android studio debug技巧个人觉得写得不错,转自:http://www.jianshu.com/p/011eb88f4e0d# Android Studio目前已经成为开发An ...
- iOS各种调试技巧豪华套餐
转载自http://www.cnblogs.com/daiweilai/p/4421340.html 目录 前言 逼优鸡 知己知彼 百战不殆 抽刀断Bug 普通操作 全局断点(Global Break ...
- 你所不知道的Android Studio调试技巧
转载:http://www.jianshu.com/p/011eb88f4e0d Android Studio目前已经成为开发Android的主要工具,用熟了可谓相当顺手.作为开发者,调试并发现bug ...
- Visual Studio原生开发的20条调试技巧(下)
我的上篇文章<Vistual Studio原生开发的10个调试技巧>引发了很多人的兴趣,所以我决定跟大家分享更多的调试技巧.接下来你又能看到一些对于原生应用程序的很有帮助的调试技巧(接着上 ...
- Visual Studio原生开发的10个调试技巧
这篇文章只介绍了一些有关Visual Studio的基本调试技巧,但是还有其他一些同样有用的技巧.我整理了一些Visual Studio(至少在VS 2008下)原生开发的调试技巧.(如果你是工作在托 ...
- xcode的调试技巧
转自:http://www.cnblogs.com/daiweilai/p/4421340.html#biyouji 目录 前言逼优鸡知己知彼 百战不殆抽刀断Bug 普通操作 全局断点(Global ...
- LTE Module User Documentation(翻译15)——示例程序、参考场景以及故障检测和调试技巧
LTE用户文档 (如有不当的地方,欢迎指正!) 21 Examples Programs(示例程序) 路径 src/lte/examples/ 包含一些示例仿真程序,这些例子表明如何仿真不 ...
- 调试技巧--Windows端口号是否被占用
调试技巧--Windows端口号是否被占用 一.端口概念 10.0.0.0~10.255.255.255,172.16.0.0~172.16.255.255, 192.168.0.0~192.168. ...
随机推荐
- thinkphp模版常量替换机制
- 监控SQLServer作业执行情况脚本
SELECT [sJOB].[job_id] AS [作业ID] , [sJOB].[name] AS [作业名] , CASE WHEN [sJOBH].[run_date] IS NULL OR ...
- Oracle工作笔记
重命名表 RENAME PUB_ORDER_DATE_RES TO PUB_ORDER_DATA_RES; 新增字段 ); 删除字段 ALTER TABLE PUB_ORDER_DATA_RES DR ...
- 域名解析A记录与CNAME有什么区别?
A记录是直接将域名指向某个IP,如果您的主机IP不常变动就建议使用A记录.而别名解析是先将域名解析到主机别名再转跳到IP这样主机IP改变了不用重新解析.如果主机IP常变建议用别名解析 A记录正规些.独 ...
- Filebeat配置paths里,不支持递归所有子目录
这个知识点要牢记哟,不然,牛B吹大了,收不回哈. 官方文档为证: Currently it is not possible to recursively fetch all files in all ...
- Adapter 适配器
意图 将一个类的接口转换成客户希望的另外一个接口.Adapter模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作. 动机 在软件开发中,有的时候系统的数据和行为都正确,但接口不符合,我们应 ...
- asp.net MVC发布iis无法加载css,js和图片
今天真够郁闷的,遇到了在本地能运行的项目到了iis服务器那里就不行了,无法加载css,js和图片,这里说清楚一下先,关于asp.net 的MVC中这样的情况其实不少,但是之前遇到的是在visual s ...
- 【AtCoder】ARC095 C-F题解
我居然每道题都能想出来 虽然不是每道题都能写对,debug了很久/facepalm C - Many Medians 排序后前N/2个数的中位数时排序后第N/2 + 1的数 其余的中位数都是排序后第N ...
- bzoj 3926 转换+广义后缀自动机
思路:重点在于叶子节点只有20个,我们把叶子节点提到根,把20个trie图插入后缀自动机,然后就是算有多少个本质不同的字串. #include<bits/stdc++.h> #define ...
- caffe 中 plot accuracy和loss, 并画出网络结构图
plot accuracy + loss 详情可见:http://www.2cto.com/kf/201612/575739.html 1. caffe保存训练输出到log 并绘制accuracy l ...