惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Attack and Defense Labs
Attack and Defense Labs
T
Threatpost
C
Cybersecurity and Infrastructure Security Agency CISA
H
Hackread – Cybersecurity News, Data Breaches, AI and More
I
Intezer
C
Cyber Attacks, Cyber Crime and Cyber Security
The Register - Security
The Register - Security
量子位
Security Latest
Security Latest
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
大猫的无限游戏
大猫的无限游戏
小众软件
小众软件
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
C
CXSECURITY Database RSS Feed - CXSecurity.com
MyScale Blog
MyScale Blog
J
Java Code Geeks
Apple Machine Learning Research
Apple Machine Learning Research
Google DeepMind News
Google DeepMind News
WordPress大学
WordPress大学
Spread Privacy
Spread Privacy
Jina AI
Jina AI
博客园 - 【当耐特】
P
Palo Alto Networks Blog
Last Week in AI
Last Week in AI
SecWiki News
SecWiki News
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
G
GRAHAM CLULEY
宝玉的分享
宝玉的分享
Hacker News - Newest:
Hacker News - Newest: "LLM"
T
The Blog of Author Tim Ferriss
V
Vulnerabilities – Threatpost
有赞技术团队
有赞技术团队
T
Tor Project blog
H
Hacker News: Front Page
A
Arctic Wolf
NISL@THU
NISL@THU
A
About on SuperTechFans
云风的 BLOG
云风的 BLOG
Engineering at Meta
Engineering at Meta
V
V2EX
N
News and Events Feed by Topic
Webroot Blog
Webroot Blog
Know Your Adversary
Know Your Adversary
P
Privacy International News Feed
I
InfoQ
D
Docker
L
LINUX DO - 最新话题
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
U
Unit 42

博客园 - 没肉包子

Vim as Python IDE on windows 如何在windows下安装"The Eric Python IDE" - 没肉包子 一个新的轮回,一个新的开始 一名英格兰球迷眼里的本届英格兰队 用我的MyGeneration模板生成NHibernate映射文件和关系(one-to-one,one-to-many,many-to-many) 强大的代码生成工具MyGeneration 请帮'Atlas'起个名字 - 没肉包子 - 博客园 NHibernate的关联映射(one-to-one,one-to-many,many-to-many)以及cascade分析 特里当选英格兰队新任队长 发疯的live.com NHibernate的Q&A(持续更新) 诚邀博客链接 推荐两款在线日程(任务)管理的站点 NHibernate的数据库连接机制分析和如何使用外部连接 NHibernate的Session管理 有关NHibernate的问题请在此提出 [调查]如果你是个讲求编码规范的程序员,而碰到给变量、方法随意命名,注释又混乱的同事时你会怎么处理??? NHibernate的灵活配置 [转载]HTTP MIME类型即HttpResponse.ContentType属性值列表
NHibernate的调试技巧和Log4Net配置
没肉包子 · 2006-08-09 · via 博客园 - 没肉包子

对新手而言NHibernate的调试事件很痛苦的事情,不知道NHibernate做了些什么,错误出现在什么地方。

我给出一些常用的调试办法希望对你有所帮助。

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