【问题标题】:NUnit & testing log4net dynamic log file locationNUnit & 测试 log4net 动态日志文件位置
【发布时间】:2011-10-01 13:58:46
【问题描述】:

我正在编写一个应用程序,用户可以在其中更改(在运行时)存储 log4net 日志的目录。目录字符串存储在 app.config 中。
当我想用 NUnit 测试是否在正确的目录中创建了日志文件时,没有创建日志文件(和相应的目录)。
在网上寻找这个问题时,我读到 NUnit 停止了日志记录,因为它使用了 log4net 本身。提供的示例告诉您创建一个额外的 .config (Test.config),其中还包含 log4net 部分,并在测试类中加载配置,我这样做了。
使用单元测试时仍然没有创建日志文件。
启动应用程序时,会按原样创建日志文件。

设置日志目录的方法:

public void MyMethod()
    {
        string logDirectory = app.Settings["LogDirectory"].Value;
        //assure that the file name can be appended to the path
        if (!logDirectory.EndsWith(@"\"))
        {
            logDirectory += @"\";
        }

        //find the rolling file appender and set its file name
        XmlConfigurator.Configure();
        Hierarchy hierarchy = (Hierarchy)LogManager.GetRepository();
        foreach (IAppender appender in hierarchy.Root.Appenders)
        {
            if (appender is RollingFileAppender)
            {
                RollingFileAppender fileAppender = (RollingFileAppender)appender;
                string logFileLocation = string.Format("{0}Estimation_Protocol_{1}.txt", 
                                                       logDirectory, EstimatorHelper.SoftwareVersionAndDateTime());
                fileAppender.File = logFileLocation;
                fileAppender.ActivateOptions();
                break;
            }
        }
        log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        log.Debug("Logging directory & file name set.");
    }

测试类:

class EstimatorTests
{
    private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
    public EstimatorTests()
    {
        FileInfo fileInfo = new FileInfo(@"%property{LogName}");
        log4net.Config.XmlConfigurator.Configure(fileInfo);
    }
    [Test]
    public void TestLoadInputPaths()
    {
        Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
        AppSettingsSection app = config.AppSettings;
        string time = DateTime.Now.Ticks.ToString();
        app.Settings["ProcessingProtocolDirectory"].Value = "C:\\thisFolderDoesntExist" + time;

        Form mainForm = new Form();
        Form.MyMethod();
        Assert.IsTrue(Directory.Exists("C:\\thisFolderDoesntExist" + time));
        //this assert fails!
    }
}

log4net 配置:

<configSections>
  <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
</configSections>
<log4net>
  <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
    <file type="log4net.Util.PatternString" value="%property{LogName}" />
    <appendToFile value="true"/>
    <rollingStyle value="Size"/>
    <maxSizeRollBackups value="5"/>
    <maximumFileSize value="10MB"/>
    <staticLogFileName value="true"/>
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%date %level %logger - %message%newline%exception%newline"/>
    </layout>
  </appender>
  <root>
    <level value="DEBUG"/>
    <appender-ref ref="RollingFileAppender"/>
  </root>
</log4net>

【问题讨论】:

  • 我不认为你在增加任何价值来测试这个。可以说,一旦您开始写入文件系统,它就不是单元测试。我不会费心测试对 app.config 或 log4net 的读/写。尝试将您的代码与其他所有内容隔离开来。通常在单元测试中创建表单表明您的逻辑与 UI 代码混合在一起(通常不进行单元测试)。所以你应该把这个逻辑重构到它自己的类中。
  • 是的,已经计划进行重构以摆脱这种形式。让我们称之为集成测试。对此进行测试的目的是确保“MyMethod”正常工作。

标签: c# nunit log4net


【解决方案1】:

我没有测试它,但我认为您需要删除对MyMethod 中的XmlConfigurator.Configure(); 的调用,因为这会覆盖您在测试类中所做的配置。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-24
    • 1970-01-01
    相关资源
    最近更新 更多