Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

log4net Configuration Section for NUnit Test Project

I am running NUnit with the project named AssemblyTest.nunit. The test calls another assembly which uses the log4net assembly. This is using nunit version 2.4.3 with the .net 2.0 framework.

In TestFixtureSetup I am calling log4net.Config.XmlConfigurator.Configure( ) and am getting the following error:

System.Configuration.ConfigurationErrorsException: Configuration system failed to initialize ---> System.Configuration.ConfigurationErrorsException: Unrecognized configuration section log4net. (C:\path\to\assembly.dll.config line 7)

Is there a way to fix this without renaming the config file to 'AssemblyTest.config'?

like image 798
Code Bytes Avatar asked Oct 01 '08 21:10

Code Bytes


2 Answers

I had the same problem because I forget to add the log4net definition in the configSections element.

So, if you want to put log4net-elements into the app.config, you need to include the configSections element (which tells where log4net-elements are defined) at the top of the config file.

Try it like this:

<configuration>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
  </configSections>
  <log4net>
  ...
  </log4net>
</configuration>
like image 174
Christoph Brückmann Avatar answered Sep 19 '22 19:09

Christoph Brückmann


I don't know why you guys are trapped in config files, for nunit if you like to see logs running in Text Output window in nunit test runner all you need to do is following line of code,

BasicConfigurator.Configure();

best point add this line is the constructor of Test class

e.g.

[TestFixture]
    public class MyTest
    {
        log4net.ILog log = log4net.LogManager.GetLogger(typeof(MyTest));

        public MyTest()
        {
            BasicConfigurator.Configure();
        }

        [SetUp]
        public void SetUp()
        {
           log.Debug(">SetUp");               
        }

        [TearDown]
        public void TearDown()
        {
            log.Debug(">TearDown");
        }

        [Test]
        public void TestNothing()
        {
            log.Debug(">TestNothing");
        }
    }
like image 42
Mubashar Avatar answered Sep 22 '22 19:09

Mubashar