40

I have the following configuration file for NHibernate:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
  <session-factory>
    <property name="connection.connection_string">Server=.\SQLEXPRESS;Database=mydb;Integrated Security=True;</property>
    <property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property>
    <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
    <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
    <property name="connection.release_mode">auto</property>
    <property name="adonet.batch_size">500</property>

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

  </session-factory>
</hibernate-configuration>

But the SQL doesn't show in the output window of Visual Studio. Is it mandatory to install log4net? Or should show_sql work alone?

5 Answers 5

60

To show the SQL in the output window of Visual Studio, configure log4net to use TraceAppender in your log4net config. This:

<appender name="DebugSQL" type="log4net.Appender.TraceAppender">
    <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
    </layout>
</appender>

Then this:

<logger name="NHibernate.SQL" additivity="false">
    <level value="DEBUG" />
    <appender-ref ref="DebugSQL" />
</logger>

EDIT: I can't seem to format this correctly here. See this link for code example

4
  • 2
    it doesn't appear to support VS2010 with .NET 4.0
    – Alex
    Commented Apr 28, 2011 at 19:27
  • 1
    @Alex it works in VS2010 with .NET4 for me, perhaps you forgot to call log4net.Config.XmlConfigurator.Configure(); in your Application_Start? (or perhaps it's since been fixed)
    – kasey
    Commented May 3, 2012 at 10:24
  • 5
    Why isn't this the accepted answer? I don't think the owner of the question was searching for paid products! Really men, SO is becaming more and more polluted by software house trying to sell their products! Stop it!
    – andreapier
    Commented Dec 17, 2012 at 15:24
  • Can I use it in Unit Test ?
    – Kiquenet
    Commented May 10, 2016 at 13:53
18

For those who prefer code rather than configuration, the following snippet will create the appropriate NH logger with a simple console appender.

var hierarchy = (Hierarchy) LogManager.GetRepository();
var logger = (Logger) hierarchy.GetLogger("NHibernate.SQL");
logger.AddAppender(new ConsoleAppender {Layout = new SimpleLayout()});
hierarchy.Configured = true;
2
  • 4
    I had to use TraceAppender instead of ConsoleAppender to see this in the Visual Studio output window.
    – Richard
    Commented Dec 10, 2013 at 9:45
  • you also must to add logger.Level = Level.Debug
    – spartaco
    Commented Jun 20, 2017 at 16:03
8

show_sql outputs to Console.Out - it's most useful when running integration tests

3
  • does it mean no way to see it in VS? Does it mean I must install log4net? Commented Jan 24, 2009 at 1:38
  • TestDriven.NET sends Console.Out to the output window in VS. That's how I use show_sql
    – Matt Hinze
    Commented Jan 26, 2009 at 16:41
  • If you run it under NUnit, it shows up in NUnit's Text Output tab. If you run it in a plain old app, methinks it should show up in a Console window. It goes wherever Console.Out goes. You should not need to do anything with log4net.
    – jyoungdev
    Commented Nov 4, 2010 at 15:02
6

Since NHibernate 3.0 you can use loquacious configuration

configuration.DataBaseIntegration(x =>
{
  x.LogSqlInConsole = true;
  x.LogFormattedSql = true;
});

Others info available at http://fabiomaulo.blogspot.com.ar/2009/07/nhibernate-configuration-through.html

1
  • what is configuration ? Not in NHibernate 3.3.0.4000
    – Kiquenet
    Commented May 10, 2016 at 13:59
4

There is something called NHibernate profiler you can use.

http://nhprof.com/

it's pricey but it works and it has a 30 day trial.

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