C# 如何配置 Fluent NHibernate 将查询输出到 Trace 或 Debug 而不是 Console?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2134565/
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
How to configure Fluent NHibernate to output queries to Trace or Debug instead of Console?
提问by André Pena
How to configure Fluent NHibernate to output queries to Trace or Debug instead of Console?
I'm using MsSqlConfiguration.MsSql2008.ShowSql()
but it has no parameters and I can't find anything on Google.
如何配置 Fluent NHibernate 将查询输出到 Trace 或 Debug 而不是 Console?我正在使用,MsSqlConfiguration.MsSql2008.ShowSql()
但它没有参数,而且我在 Google 上找不到任何内容。
采纳答案by mindplay.dk
I can see from forum and blog posts everywhere that lots of others before me have looked for a way to get the SQL statements as they're being prepared for execution. The answer typically is something along the lines of "you can't", or "you shouldn't".
我可以从到处的论坛和博客文章中看到,在我之前的许多其他人都在寻找一种方法来获取准备执行的 SQL 语句。答案通常类似于“你不能”或“你不应该”。
Whether I should or not, that's what I wanted.
不管我该不该,这就是我想要的。
After hours of searching, investigation and failed attempts, and finally I came up with this.
经过数小时的搜索、调查和失败的尝试,我终于想出了这个。
Write up an interceptor:
编写一个拦截器:
using NHibernate;
using System.Diagnostics;
public class SqlStatementInterceptor : EmptyInterceptor
{
public override NHibernate.SqlCommand.SqlString OnPrepareStatement(NHibernate.SqlCommand.SqlString sql)
{
Trace.WriteLine(sql.ToString());
return sql;
}
}
Of course, you don't have to Trace.WriteLine()
here, you could write it to a log file, or whatever else you need.
当然,您不必在Trace.WriteLine()
这里,您可以将其写入日志文件,或者您需要的任何其他内容。
In your connection manager, hook up your Interceptor like so:
在你的连接管理器中,像这样连接你的拦截器:
protected virtual void Configure(FluentConfiguration config)
{
config.ExposeConfiguration(x =>
{
x.SetInterceptor(new SqlStatementInterceptor());
});
}
It's not that complicated. From my perspective, certainly easier than trying to get all this XML pushed through Fluent to NHibernate - since Fluent abstracts the XML file away.
没那么复杂。从我的角度来看,肯定比尝试将所有这些 XML 通过 Fluent 推送到 NHibernate 更容易 - 因为 Fluent 将 XML 文件抽象化了。
Keep in mind, you can only have a single Interceptor - so you may need to integrate this feature with your existing Interceptor, if you already have one. On that note, you might want to give it a broader name - e.g. MyAppInterceptor, so as not to imply a specific purpose, because you may want to add other features to it later.
请记住,您只能拥有一个 Interceptor - 因此您可能需要将此功能与现有的 Interceptor 集成(如果您已经拥有)。在那一点上,您可能想给它一个更广泛的名称 - 例如 MyAppInterceptor,以免暗示特定目的,因为您可能希望稍后向其添加其他功能。
Hope this is helpful to somebody else! :-)
希望这对其他人有帮助!:-)
回答by Michael Maddox
You probably want to use log4net, not ShowSql. Here is some configuration to send queries to Debug:
您可能想使用 log4net,而不是 ShowSql。以下是将查询发送到 Debug 的一些配置:
<configSections>
<section name="log4net"
type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
</configSections>
<log4net debug="false">
<appender name="WindowsDebugOutput" type="log4net.Appender.DebugAppender,
log4net">
<layout type="log4net.Layout.PatternLayout,log4net">
<param name="ConversionPattern"
value="%d{ABSOLUTE} %-5p %c{1}:%L - %m%n" />
</layout>
</appender>
<logger name="NHibernate.SQL" additivity="false">
<level value="DEBUG" />
<appender-ref ref="WindowsDebugOutput" />
</logger>
</log4net>
And then call this from your code before opening an NHibernate session:
然后在打开 NHibernate 会话之前从您的代码中调用它:
log4net.Config.XmlConfigurator.Configure();
When you add a reference to the log4net DLL, make sure to set its "Copy Local" property to "true".
添加对 log4net DLL 的引用时,请确保将其“Copy Local”属性设置为“true”。
This isn't specific to FluentNHibernate, it works the same in any variant of NHibernate.
这不是 FluentNHibernate 特有的,它在 NHibernate 的任何变体中都是一样的。
回答by Tom Bushell
I have not tried this with SQL Server, but with SQLite, the following code will show generated SQL in the Outputwindow (Debug menu -> Windows -> Output, in VS2008).
我没有在 SQL Server 上尝试过这个,但是对于 SQLite,下面的代码将在输出窗口(调试菜单 -> Windows -> 输出,在 VS2008 中)中显示生成的 SQL 。
The "Show output from:" combo box in the Output window should be set to "Debug" - VS2008 did that for me automatically.
输出窗口中的“显示输出:”组合框应该设置为“调试”——VS2008 自动为我做了。
sessionFactory = Fluently.Configure()
.Database(SQLiteConfiguration.Standard
.UsingFile(DbFile)
// Display generated SQL in Output window
.ShowSql()
)
.Mappings(m => m.AutoMappings.Add( GetAutoPersistenceModel() ))
.BuildSessionFactory()
;
A word of warning - turning this on can slow down execution considerably.
一个警告——打开它会大大减慢执行速度。