如何在Visual Studio中读取NHibernate生成的sql

发布于 2024-12-17 18:47:34 字数 492 浏览 4 评论 0原文

据我所知,读取NHibernate生成的sql脚本有3种方式: 1.log4net 2.sql分析器 3. show_sql = true

这里我只想说第三点,据说可以在Visual Studio的输出窗口中读取sql。但无论我做什么,我都看不到任何东西?!

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

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

  1. 可以显示“Console.WriteLine()”的结果吗?
  2. “show_sql=true”可以让sql脚本显示出来吗?

如果是,怎么办?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

悲欢浪云 2024-12-24 18:47:34

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

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

<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>

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文