How to log SQL calls with NHibernate to the console of Visual Studio?

前端 未结 5 1125
小蘑菇
小蘑菇 2020-12-02 11:21

I have the following configuration file for NHibernate:




        
相关标签:
5条回答
  • 2020-12-02 11:44

    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.

    0 讨论(0)
  • 2020-12-02 11:47

    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

    0 讨论(0)
  • 2020-12-02 11:48

    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

    0 讨论(0)
  • 2020-12-02 11:51

    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;
    
    0 讨论(0)
  • 2020-12-02 12:07

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

    0 讨论(0)
提交回复
热议问题