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

8
  • Log4net is already used by NHibernate and logs the sql statements
    – Paco
    Commented Mar 18, 2010 at 17:14
  • How do you want to view the console in a web app?
    – Paco
    Commented Mar 18, 2010 at 17:18
  • I want to show on the server side a console application that shows the generated sql like the SQL Server Profiler or Ayends NHibernate Logger.
    – Rookian
    Commented Mar 18, 2010 at 17:34
  • 1
    I STRONGLY recommend you do not run ShowSQL in production. It can GREATLY slow down execution. Commented Mar 18, 2010 at 17:57
  • 1
    So you mean you want to view the log in a console application? In that case just parse the log in the console application.
    – Paco
    Commented Mar 18, 2010 at 18:19

1 Answer 1

2

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 };
3
  • 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
    Commented Mar 18, 2010 at 17:09
  • 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
    Commented Mar 19, 2010 at 5:16
  • @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. Commented Mar 19, 2010 at 5:51

Not the answer you're looking for? Browse other questions tagged or ask your own question.