When developing your data access layer with NHibernate you are bound to run into some pretty nasty exceptions. Many times the exceptions may be nasty, but at least they explain the problem in enough detail to allow you to solve the problem. But what do we do when the exceptions do not convey enough information to allow us to simply and easily spot our issue? Simply, time to bust out the shovel and pick ax and start digging for the nugget of information which will allow you to find your solution.
The good news is there is at least 2 (could be many more) ways you can get detailed information on what NHibernate is doing and what possible be wrong.
Utilize NHibernate Profiler
If you are using NHibernate and you are NOT using NHProfiler then you are basically writing code with one hand tied behind your back. This tool can and will help you solve so many problems you will be amazed.
But when hunting bugs NHProf is practically useful because it gives you a very clean way to see the exact sql which is being generated and executed in real time (or damn close to it). Implementing NHprof could not be easier and takes a mere 3 steps and 1 line of code.
To Implement all you do is
- Add reference to HibernatingRhinos.NHibernate.Profiler.Appender.dll class in your project.
- Add this one line of code at the entry point of your application (or unit test)
HibernatingRhinos.NHibernate.Profiler.Appender.NHibernateProfiler.Initialize();
- Launch the GUI HibernatingRhinos.NHibernate.Profiler.Client.exe
Utilize the logging which is build into NHibernate via Log4Net
Now sometimes even NHProfiler is not going to solve your problems and when this is the case you are in for some fun. I have found the best way to debug what is going on is to turn on logging for Nhibernate. Now I am not going to go into depth on how to do this as post on Hibernateing Rhinos and Nhiberate.org do a great job of providing this information.
Once you have your app.config or your web.config setup all you need to do is add this line of code at the entry point of your application (or unit tests)
log4net.Config.XmlConfigurator.Configure()
When implementing logging with Log4Net I would suggest 2 things
- Turn on DEBUG logging, this will produce a ton of content but is needed
- If using a file appender up the max file size to be at least 1mb. Anything lower will simply produce too many files and you will be swimming in a see of log files
If you utilize the above 2 techniques for trouble shooting/debugging your nhibernate issues you should be able to resolve most of them in no time flat.
Till next time,
Posted
05-05-2009 4:14 AM
by
Derik Whittaker