开发者

NHibernate: How to show the generated SQL-statements in the windows console

 return Fluently.Configure()
   开发者_运维知识库             .Database(MsSqlConfiguration.MsSql2008
                              .ConnectionString(c => c
                                                         .Database(Database)
                                                         .TrustedConnection()
                                                         .Server(Server)
                              ).ShowSql())
                .ExposeConfiguration(c => c.SetProperty("current_session_context_class", "web"))
                .Mappings(m => m.FluentMappings.AddFromAssemblyOf<TeamMap>()).BuildConfiguration();

I have a web application. This configuration does not work.

I also have a Console application that shall be responsible to write out all generated SQL.

How can I get the generated SQL commands?

Thanks in advance!


Since NHibernate already logs SQL via log4net, that is the easiest approach. As you don't want log files, configure the trace appender and view the results via the usual methods for ASP.NET Trace. By configuring in code, you can be sure it is gone when you no longer need it.

var appender = new log4net.Appender.AspNetTraceAppender();
appender.Layout = new log4net.Layout.PatternLayout{ ConversionPattern="%-5level - %message%newline" };
appender.Threshold = log4net.Core.Level.Info;
log4net.Config.BasicConfigurator.Configure( appender );

If you just want the SQL statements, you only want messages from NHibernate.Loader.Loader at Info level.

Trace is a logging facility within ASP.NET, the results of which can be seen either at the end of the page that generated the messages, or via ~/trace.axd

If the trace output is too verbose for you needs, or you don't want to go that way for any reason, there are other appenders that can send the log messages over the network.

The UDPAppender sends log message over the network via UDP.

The TelnetAppender lets you connect to log4net via telnet. To view the messages, you can telnet to your application from a console window.

var appender = new log4net.Appender.TelnetAppender{ Port=23 };
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜