View SQL at runtime

4

Is there any monitor that shows me the sql script at runtime?

For example, during the debug, I would like to see how the script was mounted by the following code:

var q = (from proc in
                 Sessao.Query<Processo>()
                 Sessao.Query<MovimentoEsparta>() where mov.DtHrMov >=
                   dtInicial && mov.DtHrMov < dtFinal && e.CodDoc == 
                     mov.CodDoc select mov.CodDoc).Any())
                 from psa in Sessao.Query<ProcessoSituacaoAtual>()
                 from mpf in Sessao.Query<EspartaMpf>()
                 where proc.CodDoc == psa.CodDoc
                 && proc.CodDoc == mpf.CodDoc
                 && proc.CodTipProc != 388
                 && proc.CodTipProc != 399
                 && proc.DtAutua >= dtInicial
                 && proc.DtAutua < dtFinal
                 select new
                 {
                     proc.NumAno,
                     proc.CodDoc,
                     DtHrAutuacao = proc.DtAutua,
                     ClasseCnj = proc.CodTipProc
                 });

Is this possible? Detail: Using NHibernate with Oracle Database with VS2013 Professional IDE (C #)

    
asked by anonymous 28.03.2016 / 14:53

1 answer

2

Yes, it is possible, but unlike Java and the implementation of Hibernate , it has no property that you simply "arrow" and SQL is shown in LOG ....

By c #, you can write / implement an interceptor and do whatever you want with the query - until you modify it in real time.

using NHibernate;
using System.Diagnostics;

public class SqlInterceptor : EmptyInterceptor
{
    public override NHibernate.SqlCommand.SqlString OnPrepareStatement(NHibernate.SqlCommand.SqlString sql)
    {
        Trace.WriteLine(sql.ToString()); //opcional, aqui você pode modificar para escrever em log, por exemplo.
        return sql;
    }
}

And in the configuration of NHibernate you "staple" the interceptor:

protected virtual void Configurar(FluentConfiguration config)
{
    config.ExposeConfiguration(x =>
                                   {
                                       x.SetInterceptor(new SqlInterceptor());
                                   });
}

Another solution is to implement via Log4j :

<configSections>
    <section name="log4net"
     type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
</configSections>

<log4net debug="false">
    <appender name="WindowsDebugOutput" type="log4net.Appender.DebugAppender,
         log4net">
<layout type="log4net.Layout.PatternLayout,log4net">
        <param name="ConversionPattern"
              value="%d{ABSOLUTE} %-5p %c{1}:%L - %m%n" />
      </layout>
</appender>

<logger name="NHibernate.SQL" additivity="false">
      <level value="DEBUG" />
      <appender-ref ref="WindowsDebugOutput" />
    </logger>
</log4net>

And when you open session of NHibernate , you "call" Log4j :

log4net.Config.XmlConfigurator.Configure();

    
28.03.2016 / 15:11