29 Jul
2008

How to show Log4Net info in NUnit (from NHibernate)

Category:UncategorizedTag: :

OK, simple little problem.  You are trying to figure out a problem with an NHibernate mapping file, you know that NHibernate uses Log4Net to log all sorts of interesting info, but you aren’t seeing it when you run your NUnit tests.

Solution:

1. Add an app.config to your test project (if there isn’t one there already) and add this code:

   1: <?xml version="1.0" encoding="utf-8" ?>
   2: <configuration>
   3:     <configSections>
   4:         <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
   5:     </configSections>
   6:  
   7:     <!-- This section contains the log4net configuration settings -->
   8:     <log4net debug="false">
   9:  
  10:         <!-- Define some output appenders -->
  11:  
  12:         <appender name="console" type="log4net.Appender.ConsoleAppender, log4net">
  13:             <layout type="log4net.Layout.PatternLayout,log4net">
  14:                 <param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />
  15:             </layout>
  16:         </appender>
  17:  
  18:         <!-- Setup the root category, add the appenders and set the default priority -->
  19:  
  20:         <root>
  21:             <priority value="DEBUG" />
  22:             <appender-ref ref="console" />
  23:         </root>
  24:     </log4net>
  25: </configuration>

This configures Log4Net to log almost everything to the console window…which NUnit keeps track of.

2. In your test class, add the following line:

   1: log4net.Config.XmlConfigurator.Configure();

OK, so now anything that is logged with Log4net will show up in NUnit. 

3. Double check your NHibernate settings (mine are in the file hibernate.cfg.xml). Make sure show_sql is true

   1: <property name="show_sql">true</property>

Regardless of the log4net status with NUnit, this will display the SQL query being executed by NHibernate in NUnit.

 

Now a word of warning, if you are having trouble with NHibernate, and you use this to get the log info out of it.  To turn off NHibernate logging, but leave logging on for everything else, add this to the app.config (around line 19 of my app.config text from above):

   1: <logger name="NHibernate">
   2:     <level value="OFF"/>
   3: </logger>
   4: <logger name="NHibernate.SQL">
   5:     <level value="OFF"/>
   6: </logger>

4 thoughts on “How to show Log4Net info in NUnit (from NHibernate)

Comments are closed.