23

I am using linq to Nhibernate to fire some select query to data base.

My question is, how do I know, the query generated by Fluent NHibernate?

2

8 Answers 8

37

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

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

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

2
  • 1
    Hi Kevin, Thanks for posting this! I'm wondering if you've also hooked up Log4Net so it sends the generated SQL to a file?
    – 5x1llz
    Commented Jun 6, 2009 at 2:04
  • 14
    This is so frustrating. Any idea why this would ever not work? I'm looking through my Console and it's just not there.
    – Milimetric
    Commented Jun 14, 2011 at 22:26
6

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

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

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


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

6

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

1
  • Whilst this may theoretically answer the question, it would be preferable to include the essential parts of the answer here, and provide the link for reference.
    – Mat
    Commented Nov 7, 2018 at 8:33
6

I have found 4 options to know sql query 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 - It is really good to see sql. we can put it in our Output of Visual Studio and even 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;
        }
     }
    
2

See this. What you need is hibernate.show_sql.

1
  • 7
    How do you configure that with Fluent NHibernate? Commented May 22, 2009 at 11:57
0

You can use sql profilers like this one too.

0

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

-1

You can also hook in log4net.

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