[ACCEPTED]-How to know query generated by Fluent NHibernate-output

Accepted answer
Score: 37

With Fluent NHibernate, you can turn on 2 show_sql like this:

Fluently.Configure()
    .Database( MsSqlConfiguration.MsSql2005.ShowSql().ConnectionString(...) )...

NHibernate will now print every 1 sql statement to Console.Out.

Score: 6

You might also find this useful http://nhprof.com/

0

Score: 6

If you want the SQL to be in log4net, make 4 sure you set the logger in your configuration 3 section.

I put the NHibernate package at 2 "INFO" to reduce the noise and the NHibernate.SQL 1 to all so I can log all SQL statements.

  
 <logger name="NHibernate">
   <level value="INFO" />
 </logger>


  <logger name="NHibernate.SQL">
    <level value="ALL" />
  </logger>

Score: 6

I have found 4 options to know sql query 4 in nhibernate and fluent nhibernate.

  1. Log - Joey V. said in answer of this same question.
  2. ShowSql - Kevin Berridge said in answer of this same question.
  3. NHProf - This is a awesome profiler. NHProf
  4. Intercepter 3 - It is really good to see sql. we can put 2 it in our Output of Visual Studio and even 1 in log file.

    ISessionFactory sf = Fluently.Configure()
            .Database(MySQLConfiguration.Standard.ConnectionString(ConnectionString).ShowSql())
            .Mappings(m => m.FluentMappings.AddFromAssemblyOf<Stock>())
            .ExposeConfiguration(c => c.SetInterceptor(new ABCInterceptor()))
            .BuildSessionFactory();
    
    
    public class ABCInterceptor : EmptyInterceptor
    {
        public override NHibernate.SqlCommand.SqlString OnPrepareStatement(NHibernate.SqlCommand.SqlString sql)
        {
           Trace.WriteLine(sql.ToString());
           return sql;
        }
     }
    
Score: 2

See this. What you need is hibernate.show_sql.

0

Score: 0

You can use sql profilers like this one too.

0

Score: 0

Definitely buy and use NHProf. This is an awesome 4 product and not only shows you what queries 3 are being run, but also shows you any potential 2 performance problems with your NHibernate 1 mappings and queries.

More Related questions