使用NUnit进行测试时写入日志文件

时间:2016-09-08 15:05:53

标签: c# log4net

我有一个测试程序集(MyTestProject),我想用log4net编写一些日志记录。因此,我创建了一个配置文件,其名称与我按照建议here设置日志记录的程序集相同:

<?xml version="1.0" encoding="utf-8" ?>

<!-- .NET application configuration file
 This file must have the exact same name as your application with
 .config appended to it. For example if your application is testApp.exe
 then the config file must be testApp.exe.config it must also be in the
 same directory as the application. -->
<configuration>
    <configSections>
    <!-- Register the section handler for the log4net section -->
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
    <sectionGroup name="NUnit">
      <!--  For .NET 2.0 Beta 2 replace the lines with the following -->
      <section name="TestCaseBuilder" type="System.Configuration.NameValueSectionHandler, System, Version=2.0.50215.44, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
      <section name="TestRunner" type="System.Configuration.NameValueSectionHandler, System, Version=2.0.50215.44, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
    </sectionGroup>
    </configSections>
    <NUnit>
      <TestCaseBuilder>
        <add key="OldStyleTestCases" value="false" />
      </TestCaseBuilder>
      <TestRunner>
        <!-- Valid values are STA,MTA. Others ignored. -->
        <add key="ApartmentState" value="STA" />
        <!-- See ThreadPriority enum for other valid values -->
        <add key="ThreadPriority" value="Normal" />
      </TestRunner>
    </NUnit>
    <appSettings>
      <add key="ApartmentState" value="STA" />
      <add key="apartment" value="STA" />
    </appSettings>

    <!-- This section contains the log4net configuration settings -->
    <log4net debug="true">

      <!-- Define some output appenders -->

      <appender name="LogFileAppender" type="log4net.Appender.FileAppender,log4net" >
        <param name="File" value="D:/data.log" />
        <param name="AppendToFile" value="false" />
        <layout type="log4net.Layout.PatternLayout,log4net">
            <param name="ConversionPattern" value="%d [%t] %-5p %c [%x] &lt;%X{auth}&gt; - %m%n" />
        </layout>
      </appender>


      <!-- Setup the root category, add the appenders and set the default priority -->
      <root>
        <level value="DEBUG" />
        <appender-ref ref="LogFileAppender" />
      </root>
    </log4net>
</configuration>

在我的代码中,我按如下方式设置日志记录:

[TestFixture]
public class MyTest
{

    private readonly ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

    [TestFixtureSetUp] 
    public void Init() 
    {
        log.Info("Something to log");
        ... 
    }
}

但是,当我运行测试时,不会创建此类文件D:/data.log。当我调试代码并向log的追加器添加监视时,我更加怀疑我得到一个空集合。

2 个答案:

答案 0 :(得分:0)

我把它发布在一起,因为它让我困了好几个小时。

我查看了文件应该保留的目录,其中有一个(显然来自早期的测试运行,因为它的时间戳不是最近的)。尝试删除该文件时,我收到一条消息,表明它正在使用nunit-agent-x86.exe。所以我在TaskManager中杀了进程并重新运行我的测试。现在一切正常,我登录文件。

答案 1 :(得分:-1)

如果您将键AppendToFile的值设置为true,则可以解决此问题。 由于记录器会将日志记录追加到现有文件,因此不会尝试删除它。