Question

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'?

Was it helpful?

Solution

Create a separate config file for log4net with root element log4net.

In TestFixtureSetup create a FileInfo object for this config file and give it as argument to log4net.Config.XmlConfigurator.Configure( ).

OTHER TIPS

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>

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");
        }
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top