views:

699

answers:

4

I'm running unit tests from inside VS2008 against a nHibernate application and would like to turn on logging during the unit tests so I can see a bit more of what is going on. I've copied and pasted another application's app.config that successfully logs nhibernate information into the unit tests app.config, but still don't get any output.

Here is my app.config for the testing project:

<log4net>

<appender name="GeneralLog" type="log4net.Appender.RollingFileAppender">
  <file value="Logs/general.txt" />
  <appendToFile value="true" />
  <maximumFileSize value="100KB" />
  <rollingStyle value="Size" />
  <maxSizeRollBackups value="5" />
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%d{HH:mm:ss.fff} [%t] %-5p %c - %m%n"  />
  </layout>
</appender>
<appender name="DataLog" type="log4net.Appender.RollingFileAppender">
  <file value="Logs/data.txt" />
  <appendToFile value="true" />
  <maximumFileSize value="100KB" />
  <rollingStyle value="Size" />
  <maxSizeRollBackups value="5" />
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%d{HH:mm:ss.fff} [%t] %-5p %c - %m%n"  />
  </layout>
</appender>

<!-- levels: DEBUG, INFO, WARN, ERROR, FATAL -->

<root>
  <level value="DEBUG"/>
  <appender-ref ref="GeneralLog" />
</root>

<logger name="NHibernate" additivity="false">
  <level value="DEBUG"/>
  <appender-ref ref="NHibernateFileLog"/>
</logger>
<logger name="Pushable.Data" additivity="false">
  <level value="DEBUG"/>
  <appender-ref ref="DataLog"/>
</logger>

</log4net>

When I run the test project, no output is created anywhere from log4net. Does the application that log4net runs under have to be a web or windows and not a testing application?

+1  A: 

if you wish to just look at the all the sql statements fired, set show_sql property to true in the Nhiberante configuration section,

e.g

<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<property name="show_sql">true</property>
</hibernate-configuration>
Sathish Naga
+1  A: 

using log4net in DEBUG will give you all NHibernate has to tell.

show_sql is a great option if you just want to see the SQL output

Configure Log4Net for use with NHibernate

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<!-- Others sections -->
<section name="log4net"
type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
</configSections>

<!-- Some others configurations -->

<!-- This section contains the log4net configuration settings -->
<log4net debug="false">

<appender name="console"
type="log4net.Appender.ConsoleAppender, log4net">
<layout type="log4net.Layout.PatternLayout,log4net">
<param name="ConversionPattern"
value="%d{ABSOLUTE} %-5p %c{1}:%L - %m%n" />
</layout>
</appender>

<!-- Setup the root category, add the appenders and set the default priority -->
<root>
<priority value="DEBUG" />
<appender-ref ref="console" />
</root>

</log4net>
</configuration>
Scott Cowan
+1 link was very helpful
g .
A: 

If you're code is logging in production but not in the test, take a look into the test directories. The tests run in a subdirectory of "testresults", that is created for every testrun. Your config file is certainly not there.

Everything that needs to be installed to the test directory needs to be declared. Add a [DeploymentItem("myfile")] to the testclass using it or go to Test -> edit test run configurations -> Local test run. Then go to "Deployment" and add the config file.

Stefan Steinegger
I took a look in the test results folder and still didn't see anything there. I even did a search over the entire solution directory and nothing is getting logged. I'll have to continue putzing around.
Josh
Just to make it clear: 1) you need to make sure that your log4net configuration really makes you code logging. Test this by starting you application by using this configuration. 2) you need to make sure that your log4net configuration is actually deployed to the testresults folder. You do this as described in my answer and test this by looking into the testresults\blabla\out folder. If it is not there, you don't need to search for log files.
Stefan Steinegger
Did you try the DeploymentItem?
Stefan Steinegger
A: 

In the Test initializer, there needs to be a call:

log4net.Config.XmlConfigurator.Configure();

Pretty lame on my part.

Josh