Enable NHibernate Logging to see the SQL Statements Executed

We had a need to see the SQL statements that NHibernate was producing.

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

in the configSections node:

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

In the configuration node:

1
2
3
4
5
6
7
8
9
10
11
12
13
<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="DEBUG"/>
    <appender-ref ref="NHibernateFileLog"/>
  </logger>
</log4net>

In the assembly.cs file add:

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