0

How do I implement an Interceptor that outputs the query WITH parameters? I have searched the web and stackoverflow for a long time now and haven´t found an answer to my problem. My Custom Interceptor-Class looks like this at the moment:

public class SQLDebugOutput : EmptyInterceptor, IInterceptor {
        public override NHibernate.SqlCommand.SqlString OnPrepareStatement(NHibernate.SqlCommand.SqlString sql) {
            System.Diagnostics.Debug.WriteLine("NH: " + sql.ToString());
            return base.OnPrepareStatement(sql);
        }
}

The parameters in the query are displayed as questionmarks though and I need them for debugging purposes. Also I´d prefer a solution not using the Log4Net Logger.

So, is it possible at all to retrieve the parameter values of a query using an interceptor at all? Maybe by overriding other methods of it?

Chris
  • 835
  • 12
  • 27
  • You mean the ShowSql() Option? – Chris Mar 14 '18 at 10:40
  • Yes, we are using Log4Net and I have read about creating a new Appender to log the nhibernate-queries - but I´d like to know if it is possible to get the parameters of the queries via Interceptor or not. – Chris Mar 14 '18 at 11:03
  • Thanks anyway - I read your answer after posting my previous comment. Any ideas on how to implement a similar functionality via interceptor? – Chris Mar 14 '18 at 11:06

1 Answers1

0

The SqlString type that's supplied as a parameter to the OnPrepareStatement method has a GetParameters() method that returns a collection of Parameter objects. Looking at the code suggests it should be possible to interrogate these objects and, hopefully, find what you're looking for.

David Osborne
  • 6,436
  • 1
  • 21
  • 35
  • Saying that... there's no obvious property on the `Parameter` type that represents the current value. – David Osborne Mar 14 '18 at 11:44
  • unfortunately sql.GetParameters() just returns the parameters as questionmarks and not the actual parameter values. I had already checked that - would have been to easy :-) – Chris Mar 14 '18 at 12:01
  • Sure. Just as I posted the answer I took a closer look and saw the same. There must be some way of getting the information as the parameter values get logged when logging's enabled. – David Osborne Mar 14 '18 at 12:04
  • that´s what I thought, too, but it seems to be quite hard actually. Any ideas? – Chris Mar 14 '18 at 13:30
  • There's an interesting idea here (https://stackoverflow.com/questions/6048835/intercept-sql-statements-containing-parameter-values-generated-by-nhibernate), looking at doing it with a subclassed database provider. – David Osborne Mar 14 '18 at 14:29