I had a similar problem. I wanted measure and log all queries that goes through NHibernate. What I did is I wrote a custom batching factory (in this case I work with oracle) but you can apply the same technique to any db:
1-) Implement batcher factory, (in this case I am extending existing factory)
public class OracleLoggingBatchingBatcherFactory : OracleDataClientBatchingBatcherFactory
{
public override IBatcher CreateBatcher(ConnectionManager connectionManager, IInterceptor interceptor)
{
return new OracleLoggingBatchingBatcher(connectionManager, interceptor);
}
}
2-) Implement the Batcher itself (in this case I am extending existing batcher). Make sure you inherit IBatcher again since we want our new methods to be called
public class OracleLoggingBatchingBatcher : OracleDataClientBatchingBatcher, IBatcher
{
.... // here override ExecuteNonQuery, DoExecuteBatch and ExecuteReader.
//You can do all kind of intercepting, logging or measuring here
//If they are not overrideable just implement them and use "new" keyword if necessary
//since we inherit IBatcher explicitly it will work polymorphically.
//Make sure you call base implementation too or re-implement the method from scratch
}
3-) Register the factory via NHibernate config:
<property name="adonet.factory_class">OracleLoggingBatchingBatcherFactory, MyAssembly</property>