tags:

views:

28

answers:

1

Currently I have some event listeners setup to log all insert/update/delete actions that happen. I just go through the list of properties and build a string to insert into an audit table.

What I'd really like to do is get the raw SQL query that NHibernate generates. Just like what NHProf shows.

How would I do this?

A: 

Put <property name="show_sql">true</property> in your config file to make it output the SQL to the console.

You can also put <property name="format_sql">true</property> if you want it PrettyPrinted.

Alternatively, you can configure log4net (logger is "NHibernate.SQL", IIRC) and send it anywhere you want.

Diego Mijelshon
How do I get it in code if it's being output to the console? I need it in code so I can insert it into the database as an audit log.
Josh Close
Did you read the last part? (BTW, I think that's what nhprof does)
Diego Mijelshon
Hmm... From Ayende's post here http://ayende.com/Blog/archive/2008/10/21/nhprof-logging-interception.aspx and looking at the files included with NHProf, i.e. HibernatingRhinos.Profiler.Appender.dll, it seems to be a custom log4net appender is being used. I'll look into that. Thanks.
Josh Close
Ok. I was able to create a custom log4net appender, hook into it via code, and I can see all the SQL statements just like they show up in NHProf. This will work, but is there a way to get the same results using event listeners instead? That would be ideal.
Josh Close
No, SQL is not exposed to event listeners (it's a lower level)
Diego Mijelshon