¿Cómo registrar llamadas SQL con NHibernate en la consola de Visual Studio?


Tengo el siguiente archivo de configuración para 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>

Pero el SQL no se muestra en la ventana de salida de Visual Studio. ¿Es obligatorio instalar log4net ? O debería show_sql trabajar solo?

Author: Peter Mortensen, 2009-01-24

5 answers

Para mostrar el SQL en la ventana de salida de Visual Studio, configure log4net para usar TraceAppender en su configuración de log4net. Esto:

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

Entonces esto:

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

EDITAR: Parece que no puedo formatear esto correctamente aquí. Consulte este enlace para ver el ejemplo de código

 57
Author: LordHits,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-01-09 20:43:07

Para aquellos que prefieren el código en lugar de la configuración, el siguiente fragmento creará el NH logger apropiado con un simple appender de consola.

var hierarchy = (Hierarchy) LogManager.GetRepository();
var logger = (Logger) hierarchy.GetLogger("NHibernate.SQL");
logger.AddAppender(new ConsoleAppender {Layout = new SimpleLayout()});
hierarchy.Configured = true;
 16
Author: Nathan Baulch,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-05-01 06:38:00

show_sql salidas a Console.Out - es más útil cuando se ejecutan pruebas de integración

 8
Author: Matt Hinze,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2009-01-24 01:04:09

Hay algo llamado NHibernate profiler que puede usar.

Http://nhprof.com/

Es caro pero funciona y tiene una prueba de 30 días.

 4
Author: Sara Chipps,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2009-01-24 01:41:47

Desde NHibernate 3.0 puede usar la configuración loquacious

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

Demás información disponible en http://fabiomaulo.blogspot.com.ar/2009/07/nhibernate-configuration-through.html

 2
Author: Fabio Maulo,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-09-01 15:59:54