image_thumb[4]By following this tutorial you can enable your ASP.NET MVC application to output the NHibernate’s SQL-queries into the web browser, in real-time. If you have an existing application, no coding is required, this is all about configuration.


When developing any application which uses NHibernate, at some point you will probably want to see the SQL-queries executed by the ORM. When that need arises, you have multiple options. You can:

  1. trace at the database level,
  2. use NHProf or
  3. use log4net to output the queries into a text file.

To just name a few options. Or, as this tutorial shows, it is possible to see the executed SQL-queries with a web browser, in real-time, by just using your ASP.NET MVC application. This can be done by combining NHibernate with log4net and Glimpse.


Glimpse can be described as the “Firebug for the server-side”. It’s an application which shows interesting details about your ASP.NET MVC applications, like the routes, executed methods and requested views. After configuration, there’s nothing else to do: Just use your website and watch the Glimpse’s UI to see how your application behaves. The Glimpse’s UI is divided between different tabs so it is easy to use and navigate.image_thumb[5]

One of the tabs inside the Glimpse is called Trace. This contains all the trace outputs which were written by your application when a page was requested. If you call Trace.WriteLine (“Hello”) in your controller, it will be shown on this tab. This tab is one of the easiest extension points of the Glimpse.

For more information on Glimpse check out the links in the end of this article.


Getting NHibernate’s SQL-queries into the Glimpse is done by configuring the log4net to write all the NHibernate’s logs using the TraceAppender.Nothing else is required. If something is written to the Trace, Glimpse will show it.

If you have an existing ASP.NET MVC 3 application which already uses NHibernate and log4net, configuring the Glimpse to show the SQL-queries is just a matter of couple copy-pastes.

1. Installing Glimpse

You need to have Glimpse installed and configured. Installation is easily done with Nuget: Install_package Glimpse

2. Configuring log4net

The web.config’s log4net-section must be changed so that NHibernate’s logs are written with a TraceAppender:

    <appender name="TraceAppender" type="log4net.Appender.TraceAppender">
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />

    <logger name="NHibernate.Loader">
      <level value="ALL" />
      <appender-ref ref="TraceAppender" />

3. Enabling the Glimpse

Point your browser to http://yourapp:port/Glimpse/Config and click the “Turn Glimpse On”. Now when you browse back into the homepage, you should see the Glimpse’s icon on the bottom right corner and clicking it will expand the UI.

And you’re all set.

It’s now just matter of using your application and watching the Trace-tab to see all the SQL-queries executed by the NHibernate.

If you don’t have an existing ASP.NET MVC 3 application, you can test this out by downloading a sample project from the GitHub https://github.com/mikoskinen/blog. The test project has log4net, (Fluent) NHibernate and Glimpse all configured and ready and it doesn’t require a database to run. All you have to do is to visit the http://site:port/glimpse/config to enable the Glimpse.

Tweaking the output

In my example I’m using NHibernate.Loader-logger. This results in a quite verbose output:


I like it because it provides me information on the performance of the SQL queries. If you want to include just the executed SQL-queries, you can make this happen by changing the NHibernate.Loader to NHibernate.SQL in your web.config:

    <logger name="NHibernate.SQL">
      <level value="ALL" />
      <appender-ref ref="TraceAppender" />


Or, if you want more information, changing the NHibernate.SQL to NHibernate will log everything:



The original idea was provided by Sam Saffron’s blog post “A day in the life of a slow page at Stack Overflow”.


Comment by cheap oakley sunglasses

cheap oakley sunglasses...

We stumbled across the same matter when initially I got seeking to work out the reason why points ended up consuming too long to help acquire with this provider. I had the social networking hook displaying within the Glass windows Task Manager as well ...

Comment by nike blazer pas cher

nike blazer pas cher...

I'd like to acquire buy the kindle keyboard or touch type involving Vector' nevertheless just how would certainly I then have the DIGITAL VIDEO DISC? is it down loadable?...

Comment by nike blazer pas cher

nike blazer pas cher...

Why do this most people claim Jango isn't very stated in the responses? There are several responses that talk about Jango. It is one of the main referenced internet site in all in the responds....

Comment by air max pas cher

air max pas cher...

Have a look at MyFiveStarMusic. com My partner and i locate great tunes and also now i'm generally updating. The farely fresh and Now i'm planning to get this started. Booking it. Thanks I prefer the thought of these types of, although Exercise just ...

Comment by air max pas cher

air max pas cher...

I actually transported our SSH key to Diet Coda using the clip-board. My spouse and i made use of PasteBot from Tapbots to grab the important thing out of the clipboard on my Apple pc as well as set the item inside the clipboard on my apple ipad....

Comment by air max 1 pas cher

air max 1 pas cher...

Can easily a person program code any on the web real world collection scanner, including Buddy Criminal. So We may load our own written text data file made up of namesscann. Right here we all leave Songs online on a website. a has learned Application!...

Comment by cheap timberland boots

cheap timberland boots...

Hey JacqueI as well have already been examining all the tweaks upon Workboxes but have made a decision using this older children they will avoid require the actual physical tip, which they are very effective from your checklist (at several level kids m...