故障检测
有时候你的映射可能存在一些错误,或者你想看你的sql语句到底是什么一回事这个时候我们就需要把 NHibernate 的日记记录打开
允许日记记录
nhibernate用的是log4net. 以下的配置中他是将信息写到 log.txt中的(程序运行的时候被锁定了你可以拷贝一份来查看)
<?
xml version="1.0" encoding="utf-8"
?>
< configuration >
< configSections >
< section name ="nhibernate" type ="System.Configuration.NameValueSectionHandler, System, Version=1.0.5000.0,Culture=neutral, PublicKeyToken=b77a5c561934e089" />
< section name ="activerecord"
type ="Castle.ActiveRecord.Framework.Config.ActiveRecordSectionHandler, Castle.ActiveRecord" />
< section name ="log4net"
type ="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</ configSections >
< activerecord >
</ activerecord >
< log4net >
<!-- Define some output appenders -->
< appender name ="trace" type ="log4net.Appender.TraceAppender, log4net" >
< layout type ="log4net.Layout.PatternLayout,log4net" >
< param name ="ConversionPattern" value ="%d [%t] %-5p %c [%x] <%P{user}> - %m%n" />
</ layout >
</ appender >
< appender name ="console" type ="log4net.Appender.ConsoleAppender, log4net" >
< layout type ="log4net.Layout.PatternLayout,log4net" >
< param name ="ConversionPattern" value ="%d [%t] %-5p %c [%x] <%P{user}> - %m%n" />
</ layout >
</ appender >
< appender name ="rollingFile" type ="log4net.Appender.RollingFileAppender,log4net" >
< param name ="File" value ="log.txt" />
< param name ="AppendToFile" value ="true" />
< 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 >
<!-- priority value can be set to ALL|INFO|WARN|ERROR -->
< priority value ="ALL" />
< appender-ref ref ="rollingFile" />
</ root >
</ log4net >
< nhibernate >
<!-- with this set to true, SQL statements will output to the console window if it's a console app -->
< add key ="hibernate.show_sql" value ="true" />
</ nhibernate >
</ configuration >
< configuration >
< configSections >
< section name ="nhibernate" type ="System.Configuration.NameValueSectionHandler, System, Version=1.0.5000.0,Culture=neutral, PublicKeyToken=b77a5c561934e089" />
< section name ="activerecord"
type ="Castle.ActiveRecord.Framework.Config.ActiveRecordSectionHandler, Castle.ActiveRecord" />
< section name ="log4net"
type ="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</ configSections >
< activerecord >
</ activerecord >
< log4net >
<!-- Define some output appenders -->
< appender name ="trace" type ="log4net.Appender.TraceAppender, log4net" >
< layout type ="log4net.Layout.PatternLayout,log4net" >
< param name ="ConversionPattern" value ="%d [%t] %-5p %c [%x] <%P{user}> - %m%n" />
</ layout >
</ appender >
< appender name ="console" type ="log4net.Appender.ConsoleAppender, log4net" >
< layout type ="log4net.Layout.PatternLayout,log4net" >
< param name ="ConversionPattern" value ="%d [%t] %-5p %c [%x] <%P{user}> - %m%n" />
</ layout >
</ appender >
< appender name ="rollingFile" type ="log4net.Appender.RollingFileAppender,log4net" >
< param name ="File" value ="log.txt" />
< param name ="AppendToFile" value ="true" />
< 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 >
<!-- priority value can be set to ALL|INFO|WARN|ERROR -->
< priority value ="ALL" />
< appender-ref ref ="rollingFile" />
</ root >
</ log4net >
< nhibernate >
<!-- with this set to true, SQL statements will output to the console window if it's a console app -->
< add key ="hibernate.show_sql" value ="true" />
</ nhibernate >
</ configuration >
在完成这个之后你必须调用这个方法
log4net.Config.XmlConfigurator.Configure();
一般写在 global.asax中
注意这个方法必须写在ActiveRecordStarter.Initialize(...)之前
你可以在log.txt中搜索你的hql语句.这样就可以在下面的几行里看到sql语句了