Pergunta

According to what I know, there are 3 ways to read the sql script generated by NHibernate: 1. log4net 2. sql profiler 3. show_sql = true

Here I just want to talk about 3rd one for it's said that I can read the sql in the output window in Visual Studio. But whatever I do, I can see nothing?!

Becasue some guy said the "show_sql = true" just means "Console.WriteLine()", so I post a question here.

I have to say I don't get what I want, so here I summarize my questions: in the output window in an web application:

  1. Can the result of "Console.WriteLine()" be shown?
  2. Can "show_sql=true" make the sql script be shown?

If yes, how?

Foi útil?

Solução

I don't think Visual Studio will show console output for a class library or website project. What I do is configure log4net to write NHibernate's SQL to a text file, then open the file in Visual Studio. With the right configuration, VS will show updates to the file by clicking in the window.

In your Web.config (or app.config), define the log4net section, have NHibernate format the SQL nicely, create a text file appender, and direct NHibernate messages there:

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

  <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
    <session-factory>
      <property name="format_sql">true</property>
    </session-factory>
  </hibernate-configuration>

  <log4net>
    <appender name="NHibernateLogFile" type="log4net.Appender.FileAppender">
      <file value="../Logs/NHibernate.log" />
      <appendToFile value="false" />
      <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date{HH:mm:ss.fff}%m%n==========%n" />
      </layout>
    </appender>

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

Then open up NHibernate.Log in Visual Studio. Because of the MinimalLock above, Visual Studio can read the file at the same time log4net is writing to it. When you click in the window, VS will reload the file. Just be sure to turn this off when you deploy the web site or application.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top