Simple logger for NHibernate 3

A logger is a very useful tool when developing a (web)application, especially when you’re using an object-relation mapping solution such as NHibernate or Entity Framework. In order to improve the performance of your application it is useful to analyze the SQL queries and other information (e.g. warnings, errors, caching) about the object-relation mapping.

Since NHibernate 3 it is possible to implement a custom logger. Log4net is not necessary anymore. In this post I’ll describe how to implement your own logger.

First create a new class and implement the IInternalLogger and ILoggerFactory interface:

In this example the ILoggerFactory will create a new instance of the CustomLogger:

In order to log messages to the debug console implement the functions of IInternalLogger like this:

That’s all for a very basic custom logger. In the web.config add the following key to the appSettings:

Leave a Reply

Your email address will not be published. Required fields are marked *