views:

232

answers:

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

+2  A: 

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 };
Lachlan Roche
sry you misunderstood me. I do not want to write the log to ASP.NET. I just have a ASP.NET MVC web site. I wanna start a console application seperately showing the generated sql.
Rookian
Rookian, Lachlan did not misunderstand you, he is presenting you with the easiest available option that you can have. a) Whether you have ASP.NET MVC or ASP.NET Webforms it will work THIS WAY, b) he is showing you the limitations of ASP.NET running on IIS. Clearly you are thinking in the Windows forms mindset when ASP.NET simply does NOT work that way. Console applications and ASP.NET applications do NOT run on the same application environment.
Jon Limjap
@Jon If @Rookian was unaware of the ASP.NET Trace facility, his assumption that I was suggesting a logger that sends things to a web site was quite understandable. Everything after the first code block has been added since then.
Lachlan Roche