1

I found a some post about difficulties found on logging sql queries when using NHibernate 3.x Indeed I'm using NHibernate 3.2 and I can't get the sql logging to work. I just need the simple logging capabilities that write in the output window when testing or debugging. It used to work in NH 2.2, but with this new version something is wrong. As this article says, I simply configured NHibernate with

<property name="show_sql">true</property>

I have this simple code:

using (var session = PersistenceManager.Istance.GetSession()) {
    var result = (from agenzia in session.Query<Agenzia>()
                 select agenzia).ToList();
    return result;    
}

But it seems that nothing is retrieved from the DB. So I don't know if nothing is logged (I'm debugging under visual studio 2010, so I expect to see something in the output window), or simply nothing is even executed for some strange reason. There's no exception anywhere, so I'm a little confused

EDITED: as requested, this is the complete configuration file for NHibernate:

<?xml version="1.0" encoding="utf-8" ?>
<!-- NHibernate Configuration -->
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
  <session-factory name="NHibernate.xlns">
    <property name="dialect">
      NHibernate.Dialect.MsSql2000Dialect
    </property>
    <property name="connection.driver_class">
      NHibernate.Driver.SqlClientDriver
    </property>
    <property name="show_sql">true</property>
  </session-factory>
</hibernate-configuration>
3
  • are you using log4net? ( it is the default ) Commented Nov 9, 2011 at 20:53
  • Did you solve the problem? I'm having a similar issue. Commented Nov 14, 2011 at 22:41
  • my problem wasn't about logging. I simply forgot to set the .hbm files as "embed as resource" in the visual studio project. Without this setting, the NHibernate engine simply didn't do nothing... that's why there was no log ;)
    – themarcuz
    Commented Nov 15, 2011 at 16:00

2 Answers 2

1

the flag show_sql, AFAIK logs on the STDOUT, so it works perfectly in unit test, or console application, but it does nothing if your app is without stdout ( ie winapp or web app ) in such a case you should enable the log NHibernate.SQL in the log4net config ( or in the alternative logger you possibly use). In a web app you can configure a trace appender in order to see the logged query in the trace window of the debugger.

5
  • I'm doing a web app, but I'm launching it in debug mode... and the debug should use stdout, AFAIK.
    – themarcuz
    Commented Nov 9, 2011 at 21:04
  • Ok, prepared a console application just to test the issue, and nothing appear on the output :/
    – themarcuz
    Commented Nov 9, 2011 at 21:19
  • try to attach the complete config you are using. show_sql actually call Console.Out so in the console it must work. Commented Nov 9, 2011 at 21:30
  • configuration seems ok. Try to enable log4net just to see what is wrong. Commented Nov 9, 2011 at 21:41
  • let us continue this discussion in chat
    – themarcuz
    Commented Nov 9, 2011 at 22:35
0

If you are using log4net, then to enable SQL logging, besides setting <property name="show_sql">true</property>, you need to set the level to INFO or DEBUG (verbose) as follows:

  <log4net>
    ...
    <!-- Print only messages of level INFO or above in 'NHibernate' package  -->
    <logger name="NHibernate">
      <level value="INFO" />
    </logger>
  </log4net>

Not the answer you're looking for? Browse other questions tagged or ask your own question.