View SQL at Runtime

Asked

Viewed 562 times

4

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

For example, during 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 even possible? Detail: Using Nhibernate with Oracle bank with VS2013 Professional IDE (C#)

1 answer

2


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

By c#, you can write/implement a browser 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 "wiretap" the Third Party:

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 the session of NHibernate, you "call" the Log4j:

log4net.Config.XmlConfigurator.Configure();

Browser other questions tagged

You are not signed in. Login or sign up in order to post.