如何在Visual Studio中读取NHibernate生成的sql [英] How to read sql generated by NHibernate in Visual Studio

查看:16
本文介绍了如何在Visual Studio中读取NHibernate生成的sql的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,读取NHibernate生成的sql脚本有3种方式:1.log4net2. sql探查器3. show_sql = true

这里我只想说第三个,因为据说我可以在 Visual Studio 的输出窗口中读取 sql.但不管我做什么,我什么也看不见?!

因为有人说show_sql = true"只是意味着Console.WriteLine()",所以我发布了一个问题此处.

我不得不说我没有得到我想要的,所以我在这里总结一下我的问题:在 Web 应用程序的输出窗口中:

  1. 可以显示Console.WriteLine()"的结果吗?
  2. show_sql=true"可以显示sql脚本吗?

如果是,如何?

解决方案

我认为 Visual Studio 不会显示类库或网站项目的控制台输出.我所做的是配置 log4net 将 NHibernate 的 SQL 写入文本文件,然后在 Visual Studio 中打开该文件.如果配置正确,VS 将通过在窗口中单击来显示文件更新.

在您的 Web.config(或 app.config)中,定义 log4net 部分,让 NHibernate 很好地格式化 SQL,创建一个文本文件 appender,并将 NHibernate 消息定向到那里:

<预><代码><配置><configSections><section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/></configSections><hibernate-configuration xmlns="urn:nhibernate-configuration-2.2"><会话工厂><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"/></布局></appender><logger name="NHibernate" additivity="false"><level value="WARN"/><appender-ref ref="NHibernateLogFile"/><logger name="NHibernate.SQL" additivity="false"><level value="DEBUG"/><appender-ref ref="NHibernateLogFile"/></配置>

然后在 Visual Studio 中打开 NHibernate.Log.由于上面的 MinimalLock,Visual Studio 可以在 log4net 写入文件的同时读取文件.当您在窗口中单击时,VS 将重新加载文件.请务必在部署网站或应用程序时关闭此功能.

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?

解决方案

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.

这篇关于如何在Visual Studio中读取NHibernate生成的sql的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆