Profiling NHibernate 3 application with NHProf
NHProf is a great too for profiling a NHibernate application. It is powerful and easy to use.
I used it for a long time, but recently I ran into troubles when I tried to capture DB queries in my newly started web application using NHibernate 3.0.
As many times before I downloaded latest NHibernate release, created a new web application, added a references to NHibernate.dll and all dependencies, added a reference to HibernatingRhinos.Profiler.Appender.dll and put a code, initializing NHProf - HibernatingRhinos.Profiler.Appender.NHibernate.NHibernateProfiler.Initialize(); in Application_Start.
After all that I expected NHPfrof be working smoothly, but all what I got was NHPfor window with no record, but indicating my application is being profiled:
It was strange!
Since it was my first application using NHibernate 3, I though it is compatibility issue of NHibernate. But then I took one of my previous applications, upgraded it to use NHibernate 3.0 - and this one was profiled perfect.
I started to look at differences between two applications. Both looked similar, except one detail - all my previous application have reference to log4net.dll.
Of course! NHProf use logging interceptions to capture NHibernate logs. It relies on log4net is exists in my application. After I added a reference to log4net.dll (and ensured it's copied to the 'bin' directory), it worked fine.
The problem had been solved, but I still was interesting, why I did not get such a problem with my previous projects. The answer was found with reflector (amazing tool, BTW).
I figured out that NHibernate 3.0 download does not contain log4net.dll, and NHibernate.dll does not reference to log4net.dll either:
It allowed me to run my application with no log4net.
If I used NHibernate 2.x with no log4net.dll , I would get an exception at runtime. That why all my previous application have explicit reference to log4net, and I never got a troubled with NHProf.
Conclusion: Don't forget to add log4net to your project, if you using NHibernate 3!