6
votes

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

3
I'm not familiar with .NET but there is nothing in the error that implies to me at least that this has anything to do with the name of the config file. It looks to me more like there is a problem with line 7 <i>in</i> your config file.TomC
Yeah, that's why I asked the question. The config file line works with the file named by the nunit project name and exactly the same text. Thanks for the point.Code Bytes

3 Answers

11
votes

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>
6
votes

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");
        }
    }
4
votes

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( ).