Turning off Log4Net Logging for NHibernate

Short Post!

In one of my previous posts I showed how to setup Log4Net to log errors and warnings.
One of the nice features of NHibernate is that it directly interfaces with Log4Net and generates all kinds of information.  If you use Log4Net, you’ll see pages of information about what your NHibernate ORM is doing when it runs.  The problem with this information is that it’s too much raw data and rapidly turns into spam rather than helpful information.  So I’m going to demonstrate how to turn off some of that output so you can see your Errors, without all the extra information.


Sample of full error reporting from NHibernate as a default:



Right after the <root> tag you can paste this into your app.config (or web.config) file:

   <logger name="NHibernate">
<level value="ERROR" />
</logger>
<logger name="NHibernate.SQL">
<level value="ERROR" />
</logger>
 
 

This will cause NHibernate to only output errors and reduce the amount of information that goes into your log file.  If your program runs without bugs, you will not see anything in your log files.

You can also use “WARN” and “DEBUG” like this:

   <logger name="NHibernate">
<level value="WARN" />
</logger>
<logger name="NHibernate.SQL">
<level value="DEBUG" />
</logger>
 

This will cause NHibernate to output the SQL that it generates, but keep other output to warning level and above.  

Here’s an example of a log file using the above configuration:

Notice that only the SQL information was sent to the log file (unless your NHibernate queries or mappings fail).

Which  brings me to the fact that NHibernate uses two loggers.  You can independently control the level of logging for NHibernate and the logging for the SQL that NHibernate generates.  This is helpful when you want to verify your LINQ or you notice a performance issue that might be caused by NHibernate generating a bad query.

OK, so maybe this wasn’t a short post after all.  You might want to tuck this information away for future use.  It could save a lot of time when trying to debug your database application.

Sample Code

You can download the sample code here: nhibernatelog4net.zip

 

0 thoughts on “Turning off Log4Net Logging for NHibernate

Leave a Reply