Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

log4net and nunit tests, most basic example

I want to try log some app messages from my app. In this very situation I just want to force nunit to work with log4net. I found some example here http://www.ofconsulting.com/PublicPortal/ofc-tech-blog/92-configure-log4net-with-nunit.html.

log4net is confugured in app.config like this:

<configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,Log4net"/>
  </configSections>

<log4net>
    <root>
      <level value="DEBUG" />
      <appender-ref ref="LogFileAppender" />
    </root>
    <appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender" >
      <param name="File" value="log.txt" />
      <param name="AppendToFile" value="true" />
      <rollingStyle value="Size" />
      <maxSizeRollBackups value="10" />
      <maximumFileSize value="10MB" />
      <staticLogFileName value="true" />
      <layout type="log4net.Layout.PatternLayout">
        <param name="ConversionPattern" value="%-5p%d{yyyy-MM-dd hh:mm:ss} – %m%n" />
      </layout>
    </appender>
  </log4net>

And in my test code is following

[TestFixture]
class DomainTests
{
    protected static readonly ILog log = LogManager.GetLogger(typeof(DomainTests));
    public void LoggingTests()
    {            
        log4net.Config.XmlConfigurator.Configure(); 
    }

    [Test]
    public void BasicLogTest()
    {
        log.Error("write my log entry already");
    }

My test is passed but nothing is written inside log.txt file. What am I doing wrong?
I just want to make it as simple as possible to store messages like entering an application, exit application. Regards.

like image 975
panjo Avatar asked Jun 14 '12 21:06

panjo


People also ask

Why log4net is used?

log4net is a tool to help the programmer output log statements to a variety of output targets. In case of problems with an application, it is helpful to enable logging so that the problem can be located. With log4net it is possible to enable logging at runtime without modifying the application binary.


3 Answers

As long as I know and used log4net with nunit, I have never used any config file, you just need to add following line in test class constructor

BasicConfigurator.Configure();

here is the full answer if you like to see the sample test class

like image 119
Mubashar Avatar answered Sep 21 '22 14:09

Mubashar


The problem is that the NUnit test runner (when run from resharper in visual studio) runs the test from another folder (it shadow copies the test assembly), so the xml configuration is not available at that point unless you specify the full config path.

You could of course use the basic configuration and specify the logging configuration in code, like:

log4net.Config.BasicConfigurator.Configure(
  new log4net.Appender.ConsoleAppender {
    Layout = new log4net.Layout.SimpleLayout()});

You should see the log output in the test output after that.

like image 25
m0sa Avatar answered Sep 22 '22 14:09

m0sa


My best guess would be for you to do something like this:

[TestFixture]
class DomainTests
{
    protected static readonly ILog log = LogManager.GetLogger(typeof(DomainTests));
    public void LoggingTests()
    {            
        log4net.Config.XmlConfigurator.Configure(); 
    }

    [Test]
    public void BasicLogTest()
    {
        log.Error("write my log entry already");
    }

    [SetUp]
    RunBeforeAnyTests()
    {
        BasicConfigurator.Configure();
    }

    [TearDown]
    RunAfterAnyTests()
    {
        // ...
    }

I'd also use the Log4Net.config file rather than the app.config file, it just seems cleaner. Here's an example log4net.config file:

<log4net>
  <!-- A1 is set to be a LogFileAppender -->
  <appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender, log4net" >
    <param name="File" value="C:\logging\log.txt" />
    <file value="c:\logging\Main" />
    <appendToFile value="true" />
    <datePattern value="yyyyMMdd'.log'" />
    <rollingStyle value="Composite" />
    <staticLogFileName value="false" />
    <maxSizeRollBackups value="-1" />
    <maximumFileSize value="500MB" />

    <!-- A1 uses PatternLayout -->
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
    </layout>
  </appender>
  <!-- Set root logger level to DEBUG and its only appender to LogFileAppender -->
  <root>
    <!--<level value="OFF" />-->
    <!--<level value="FATAL" />-->
    <!--<level value="ERROR" />-->
    <!--<level value="WARN" />-->
    <!--<level value="INFO" />-->
    <level value="DEBUG" />
    <!--<level value="ALL" />-->
    <appender-ref ref="LogFileAppender" />
  </root>
</log4net>
like image 20
Faraday Avatar answered Sep 24 '22 14:09

Faraday