tags:

views:

2294

answers:

6

How do I view the SQL that is generated by nHibernate? version 1.2

+5  A: 

Use sql server profiler.

EDIT (1 year later): As @Toran Billups states below, the NHibernate profiler Ayende wrote is very very cool.

IainMH
Yep a lot easier than wading through MB of log files.
Chris S
+6  A: 

In the configuration settings, set the "show_sql" property to true. This will cause the SQL to be output in NHibernate's logfiles courtesy of log4net.

Ian Nelson
Nice. I forgot about that. *click*
IainMH
+3  A: 

There is a good reference for NHibernate logging at: How to configure Log4Net for use with NHibernate. It includes info on logging all NHibernate-generated SQL statements.

Sean Carpenter
+13  A: 

You can put something like this in your app.config/web.config file :

in the configSections node :

<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/>

in the configuration node :

<log4net>
  <appender name="NHibernateFileLog" type="log4net.Appender.FileAppender">
    <file value="logs/nhibernate.txt" />
    <appendToFile value="false" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%d{HH:mm:ss.fff} [%t] %-5p %c - %m%n"  />
    </layout>
  </appender>
  <logger name="NHibernate.SQL" additivity="false">
    <level value="INFO"/>
    <appender-ref ref="NHibernateFileLog"/>
  </logger>
</log4net>

And don't forget to call

log4net.Config.XmlConfigurator.Configure();

at the startup of your application, or to put

[assembly: log4net.Config.XmlConfigurator(Watch=true)]

in the assemblyinfo.cs

In the configuration settings, set the "show_sql" property to true.

mathieu
+1  A: 

Nhibernate Profiler is an option, if you have to do anything serious.

Dan
+1  A: 

You can also try NHibernate Profiler (30 day trial if nothing else). This tool is the best around IMHO.

This will not only show the SQL generated but also warnings/suggestions/etc

Toran Billups
Deffo - +1. It didn't exist when this question was asked!
IainMH