visual-studio 如何将 NHibernate 的 SQL 调用记录到 Visual Studio 的控制台?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/474659/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 10:05:34  来源:igfitidea点击:

How to log SQL calls with NHibernate to the console of Visual Studio?

visual-studionhibernatelogginglog4net

提问by Nicolas Cadilhac

I have the following configuration file for NHibernate:

我有以下NHibernate配置文件:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
  <session-factory>
    <property name="connection.connection_string">Server=.\SQLEXPRESS;Database=mydb;Integrated Security=True;</property>
    <property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property>
    <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
    <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
    <property name="connection.release_mode">auto</property>
    <property name="adonet.batch_size">500</property>

    <property name="show_sql">true</property>

  </session-factory>
</hibernate-configuration>

But the SQL doesn't show in the output window of Visual Studio. Is it mandatory to install log4net? Or should show_sqlwork alone?

但是 SQL 不会显示在 Visual Studio 的输出窗口中。必须安装log4net吗?还是应该show_sql单独工作?

回答by LordHits

To show the SQL in the output window of Visual Studio, configure log4net to use TraceAppender in your log4net config. This:

要在 Visual Studio 的输出窗口中显示 SQL,请将 log4net 配置为在 log4net 配置中使用 TraceAppender。这:

<appender name="DebugSQL" type="log4net.Appender.TraceAppender">
    <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
    </layout>
</appender>

Then this:

然后这个:

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

EDIT: I can't seem to format this correctly here. See this link for code example

编辑:我似乎无法在这里正确格式化。有关代码示例,请参阅此链接

回答by Nathan Baulch

For those who prefer code rather than configuration, the following snippet will create the appropriate NH logger with a simple console appender.

对于那些更喜欢代码而不是配置的人,以下代码段将使用简单的控制台附加程序创建适当的 NH 记录器。

var hierarchy = (Hierarchy) LogManager.GetRepository();
var logger = (Logger) hierarchy.GetLogger("NHibernate.SQL");
logger.AddAppender(new ConsoleAppender {Layout = new SimpleLayout()});
hierarchy.Configured = true;

回答by Matt Hinze

show_sqloutputs to Console.Out- it's most useful when running integration tests

show_sql输出到Console.Out- 在运行集成测试时最有用

回答by Sara Chipps

There is something called NHibernate profiler you can use.

您可以使用一种叫做 NHibernate 分析器的东西。

http://nhprof.com/

http://nhprof.com/

it's pricey but it works and it has a 30 day trial.

它很贵,但它有效并且有 30 天的试用期。

回答by Fabio Maulo

Since NHibernate 3.0 you can use loquacious configuration

由于 NHibernate 3.0 你可以使用 loquacious 配置

configuration.DataBaseIntegration(x =>
{
  x.LogSqlInConsole = true;
  x.LogFormattedSql = true;
});

Others info available at http://fabiomaulo.blogspot.com.ar/2009/07/nhibernate-configuration-through.html

其他信息可在http://fabiomaulo.blogspot.com.ar/2009/07/nhibernate-configuration-through.html 获得