Castle ActiveRecord:查看 SQL 的最简单方法是什么?

发布于 2024-11-19 21:12:05 字数 1053 浏览 3 评论 0原文

我试图让 Castle ActiveRecord 向我展示它生成的 SQL。我在这方面找到的各种博客提供了两种替代方案:

(1) 使用 NHibernate“show_sql”设置。问题是,我正在使用编程配置,就像这样。

var config = XmlConfigurationSource.Build(
    DatabaseType.MsSqlServer2008, Settings.Default.StationManagerDbConnectionString);
config.IsRunningInWebApp = isRunningInWebApp;
config.PluralizeTableNames = true;
var modelAssembly = Assembly.GetAssembly(typeof(OneOfMyClasses));
ActiveRecordStarter.Initialize(modelAssembly, config);

通过编程配置,似乎没有办法指定“show_sql”。

(2) 使用log4net。但撇开 log4net 工作起来的痛苦不谈,我还没有找到一种方法来获取 SQL。我得到了大量的调试数据,其中 SQL 语句只是一小部分。

那么:有什么方法可以保留 Castle ActiveRecord 的编程配置,同时让 NHibernate 只输出 SQL?

编辑:这是我使用 log4net 的工作。但在我的 Web 应用程序的前两页中,调试窗口中会输出超过 14,000 行。如何更改此代码以仅获取 SQL?

var appender = new log4net.Appender.DebugAppender
{
    Layout = new log4net.Layout.SimpleLayout(),
    Name = "NHibernate.SQL",
    Threshold = log4net.Core.Level.Debug
};
log4net.Config.BasicConfigurator.Configure(appender);

I'm trying to get Castle ActiveRecord to show me the SQL it generates. The various blogs I've found on this give two alternatives:

(1) Use the NHibernate "show_sql" setting. The trouble is, I'm using programmatic configuration, like this.

var config = XmlConfigurationSource.Build(
    DatabaseType.MsSqlServer2008, Settings.Default.StationManagerDbConnectionString);
config.IsRunningInWebApp = isRunningInWebApp;
config.PluralizeTableNames = true;
var modelAssembly = Assembly.GetAssembly(typeof(OneOfMyClasses));
ActiveRecordStarter.Initialize(modelAssembly, config);

With programmatic configuration, there doesn't seem to be a way to specify "show_sql".

(2) Use log4net. But leaving aside what a pain log4net is to get working, I haven't found a way to get just the SQL. I get gobs and gobs of debug data, of which the SQL statements are just a small part.

So: Is there some way I can keep my programmatic configuration of Castle ActiveRecord but also get NHibernate to output just the SQL?

EDIT: Here's what I got to work with log4net. But in the first two pages of my web app, this spits out over 14,000 lines in the Debug window. How do I change this code to get only the SQL?

var appender = new log4net.Appender.DebugAppender
{
    Layout = new log4net.Layout.SimpleLayout(),
    Name = "NHibernate.SQL",
    Threshold = log4net.Core.Level.Debug
};
log4net.Config.BasicConfigurator.Configure(appender);

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

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

发布评论

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

评论(2

凡尘雨 2024-11-26 21:12:05

您应该能够利用 NHibernate.SQL 记录器分离出 sql。

配置示例:

<log4net>
<!-- This is a default logger that nhibernate uses to push out all the SQL statements to-->
<logger name="NHibernate.SQL" additivity="false">
  <level value="DEBUG"/>
  <appender-ref ref="NHibernateSQLLog"/>
</logger>

<!-- This is a default logger that nhibernate uses to push out all the debugging type information to-->
<logger name="NHibernate" additivity="false">
  <level value="DEBUG"/>
  <appender-ref ref="NHibernateFileLog"/>
</logger>

<appender name="NHibernateFileLog" type="log4net.Appender.RollingFileAppender">
  <file value="Logs/nhibernate.txt" />
  <appendToFile value="true" />
  <rollingStyle value="Size" />
  <maxSizeRollBackups value="10" />
  <maximumFileSize value="10MB" />
  <staticLogFileName value="true" />
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%d{HH:mm:ss.fff} [%t] %-5p %c - %m%n"  />
  </layout>
</appender>

<appender name="NHibernateSQLLog" type="log4net.Appender.RollingFileAppender">
  <file value="Logs/nhibernate_sql.txt" />
  <appendToFile value="true" />
  <rollingStyle value="Size" />
  <maxSizeRollBackups value="10" />
  <maximumFileSize value="10MB" />
  <staticLogFileName value="true" />
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%d{HH:mm:ss.fff} [%t] %-5p %c - %m%n"  />
  </layout>
 </appender>
 </log4net>

编辑:

var filter = new log4net.Filter.LoggerMatchFilter();
filter.LoggerToMatch = "NHibernate.SQL";
filter.AcceptOnMatch = true;

var filterDeny = new log4net.Filter.DenyAllFilter();

var appender = new log4net.Appender.DebugAppender
{
    Layout = new log4net.Layout.SimpleLayout(),
    Name = "NHibernate.SQL",
    Threshold = log4net.Core.Level.Debug
};

appender.AddFilter(filter);
appender.AddFilter(filterDeny);
log4net.Config.BasicConfigurator.Configure(appender);

You should be able to just separate out the sql by utilizing the NHibernate.SQL logger.

Example config:

<log4net>
<!-- This is a default logger that nhibernate uses to push out all the SQL statements to-->
<logger name="NHibernate.SQL" additivity="false">
  <level value="DEBUG"/>
  <appender-ref ref="NHibernateSQLLog"/>
</logger>

<!-- This is a default logger that nhibernate uses to push out all the debugging type information to-->
<logger name="NHibernate" additivity="false">
  <level value="DEBUG"/>
  <appender-ref ref="NHibernateFileLog"/>
</logger>

<appender name="NHibernateFileLog" type="log4net.Appender.RollingFileAppender">
  <file value="Logs/nhibernate.txt" />
  <appendToFile value="true" />
  <rollingStyle value="Size" />
  <maxSizeRollBackups value="10" />
  <maximumFileSize value="10MB" />
  <staticLogFileName value="true" />
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%d{HH:mm:ss.fff} [%t] %-5p %c - %m%n"  />
  </layout>
</appender>

<appender name="NHibernateSQLLog" type="log4net.Appender.RollingFileAppender">
  <file value="Logs/nhibernate_sql.txt" />
  <appendToFile value="true" />
  <rollingStyle value="Size" />
  <maxSizeRollBackups value="10" />
  <maximumFileSize value="10MB" />
  <staticLogFileName value="true" />
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%d{HH:mm:ss.fff} [%t] %-5p %c - %m%n"  />
  </layout>
 </appender>
 </log4net>

Edit:

var filter = new log4net.Filter.LoggerMatchFilter();
filter.LoggerToMatch = "NHibernate.SQL";
filter.AcceptOnMatch = true;

var filterDeny = new log4net.Filter.DenyAllFilter();

var appender = new log4net.Appender.DebugAppender
{
    Layout = new log4net.Layout.SimpleLayout(),
    Name = "NHibernate.SQL",
    Threshold = log4net.Core.Level.Debug
};

appender.AddFilter(filter);
appender.AddFilter(filterDeny);
log4net.Config.BasicConfigurator.Configure(appender);
岁月打碎记忆 2024-11-26 21:12:05

完成这项任务的最佳工具是 NHibernate Profiler (http://nhprof.com/)。
这是一个商业应用程序,但您可以免费试用 30 天。

The very best tool for the task is the NHibernate Profiler ( http://nhprof.com/ ).
It's a commercial application but you get 30 days free trial.

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