Fake binary trees
... rants, ramblings and occasional good idea ...

Poor man's NHibernate profiler

First of all, if you do any serious work with NHibernate, consider using the excellent NHibernate profiler. It will save you time and pinpoint many non-obvious issues with your NHibernate usage patterns.

Now, if all you need is a way to check what SQL has your web application been executing (e.g to optimize lazy loading, check for N+1 issues, etc), it is helpful if you can see this information straight on the web page which executed the statements.

For my web development work, I prefer using Castle Monorail (+ ActiveRecord or NHibernate) I have attached a demo project (see the bottom of the post) which you can feel free to use with your application. It shows how to trace NHibernate log data into the web page by using Monorail filter which collects the NHibernate information (by reading from log4net in-memory appender which receives NHibernate.SQL logging), and a component which displays the collected data.

In order to use this in your application, you need to:

  • copy DebuggingFilter and DebuggingComponent into your project
  • register DebuggingFilter with your controllers
  • set up the log4net in-memory appender (see web.config in the sample)
  • set DebuggingFilter.DebuggingActive to true at some point (e.g OnApplicationStart)
  • copy and include jQuery JavaScript in your pages (if you want to toggle the details)
  • copy a part of the stylesheet which formats debugging div (or roll your own)
  • if some step is missing, please drop me a comment

If time allows me, I would like to implement following features:

  • providing an implementation for Asp.Net MVC (actually, I already have it)
  • setting a global/per-request limit of SQL statements, which triggers an error information
  • filtering who can see the data (for use on live site, but there are issues with logging all the SQL)
  • an HttpModule implementation - need no setting up, independent of framework (Monorail, Asp.Net MVC)
  • extract CSS for debug window into separate file
  • use only minimum JavaScript instead of full jQuery

It would also be great if Oren would provide similar functionality in NHProf. E.g. you would just embed the URL of NHProf service into your pages, and the service would return the requested data.

Here's how it looks in action:

When a request is executed, the summary of the SQL is shown at the top. If there was a redirect, each request will have separate entry.



Clicking the 'toggle details' button will display the executed statements:



Download Monorail sample (uses ActiveRecord for DB access): demoapp.zip (1.87 mb)

Posted at 07:09 on January 29, 2010
Categories: .NET | Monorail | NHibernate   E-mail | del.icio.us | Permalink | Comments (1) | Post RSSRSS comment feed