I think the title is fairly self explanatory, but to give a little context.
I've been reading this question, but it doesn't quite meet my use case
Intercept-sql-statements-containing-parameter-values-generated-by-nhibernate
The issue that I have is that I want to log out the generated SQL only when an exception occurs (or a time period elapses). This is to enable us to troubleshoot some long running queries in a very complex application. I don't want to intercept and log all the queries as there would be a performance hit on this I believe.
At the point where I want to log it, I have the ICriteria object that NHibernate uses, so what I'm looking for is a kind of "dummy" NHibernate object that I can fire these ICriteria at and get back the SQL string (or even the SQL with placeholders for the parameters and a list of parameters).
So does something exist that does this (I'm thinking that it's either incredibly simple and I'm missing something, or it's very hard and nobody else needs it).
If it doesn't exist, what's the best way to go about creating something that achieves the goal, without manually building the SQL (I want to be as close to the executed SQL as possible). Is there a way to go straight to the NHibernate generator and get the string?
Adding to this to make it clearer.
I want to do this "After the fact", and not intercept every query (due to performance concerns). I also don't want to do this using Log4Net as I need it to be logged against the session.
I'm thinking that there must be a way to create a second NHibernate object that maybe uses a different driver (one that doesn't go to the database), this way I can implement a way to intercept that object, and not main object that is persisting to the database