【问题标题】:NLog with VS 2008 Unit TestNLog 与 VS 2008 单元测试
【发布时间】:2011-09-27 21:25:54
【问题描述】:

我正在尝试在我的 VS 单元测试运行时在日志文件 (Log.Trace / Log.Debug) 中记录一些条目。我还通过类的 DeploymentItem 属性将文件 NLog.config 复制到 out 目录。仍然没有创建我的日志文件。关于我们如何在文件中记录条目的任何帮助,就像我们为普通 Web 应用程序所做的那样。

【问题讨论】:

    标签: unit-testing file nlog


    【解决方案1】:

    我刚刚找到了解决这个问题的方法: 将 App.config 文件添加到您的单元测试项目中,内容如下:

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <configSections>
        <section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog"/>
      </configSections>
    
      <nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    
        <targets>
          <target name="debugLog" xsi:type="Console" />
        </targets>
    
        <rules>
          <logger name="*" minlevel="Debug" writeTo="debugLog"></logger>
        </rules>
    
      </nlog>
    
    </configuration>
    

    你可以像 NLog.config 文件一样放置任何你想要的配置。

    【讨论】:

      【解决方案2】:

      不幸的是,这是当时唯一的 XML 解决方案。不过,这不仅仅是关于单元测试。 NLog.config 不适用于任何控制台应用程序。

      不知道这是设计使然还是疏忽。无论如何,我不会说将 NLog.config 移动到 App.config 部分是令人满意的 =/

      也许值得注意的是,可以直接从代码中配置 nlog,这在某些情况下可能会有所帮助。 也可以很高兴找到 nlog 调试选项,它将记录处理配置文件、注册目标等的整个过程...

      要打开它,只需将 internalLogFile 和 internalLogLevel 属性添加到 nlog 元素:

      <nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" internalLogFile="C:/logs/nlog.txt" internalLogLevel="Debug">
      

      或来自代码:

          InternalLogger.LogFile = @"c:\logs\nlog.txt";
      
          InternalLogger.LogLevel = LogLevel.Trace;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2010-09-17
        • 1970-01-01
        • 2018-08-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-09-10
        相关资源
        最近更新 更多