log4net Configuration Section for NUnit Test Project

与世无争的帅哥 提交于 2019-12-23 11:48:05

问题


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


回答1:


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




回答2:


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>



回答3:


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


来源:https://stackoverflow.com/questions/159856/log4net-configuration-section-for-nunit-test-project

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!