Logging NHibernate SQL queries

后端 未结 4 1086
执笔经年
执笔经年 2021-02-07 14:25

Is there a way to access the full SQL query, including the values, inside my code?

I am able to log SQL queries using log4net:



        
相关标签:
4条回答
  • 2021-02-07 14:43

    Use a log4net appender with specific target (afair it supports being toggled on/off) or just extend it and toggle it within your try-catch-finally-off.

    0 讨论(0)
  • 2021-02-07 14:49

    You can override driver:

    public class LoggerSqlClientDriver:SqlClientDriver, IEmbeddedBatcherFactoryProvider
    {      
        public override void AdjustCommand(IDbCommand command)
        {
            //log here
            base.AdjustCommand(command);
        }
    
        //protected override void OnBeforePrepare(IDbCommand command)
        //{
        //    //log here
        //    base.OnBeforePrepare(command);
        //}
    }
    

    And then use it in configuration:

    var config = Fluently.Configure().
                Database(MsSqlConfiguration.MsSql2005.Driver<LoggerSqlClientDriver>();
    
    0 讨论(0)
  • 2021-02-07 14:54

    you can use an interceptor to do this:

    public class LoggingInterceptor : EmptyInterceptor {
        public override SqlString OnPrepareStatement(SqlString sql) {
    
            Debug.WriteLine(sql);
    
            return sql;
        }
    }
    

    See Nhibernate Docs for the different ways to register it with nhibernate.

    0 讨论(0)
  • 2021-02-07 14:57

    Either use sql profiler or have a look at nhprof at http://nhprof.com/

    Both will let you see sql output.

    Also set the show_sql property in hibernate config file

    <property name="show_sql">true</property>
    
    0 讨论(0)
提交回复
热议问题