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)。
读取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。
日志记录代码

ilog applicationinfolog=applicationinfolog = logmanager.getlogger("applicationinfolog");

applicationinfolog.info("记录日志");
此例中nhibernater日志会存在log.txt,系统日志记录在log1.txt。
4.在使用sqlserver时使用事件查看器监视sql
对于sqlserver数据库,假如你没有条件可以完成上面的功能,那么你可以使用事件查看器来监视执行的sql,使用比较简单,不多说了。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 25岁的心里话
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现