5

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

4

2 回答 2

12

我刚刚找到了解决此问题的方法:将 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 文件中。

于 2010-02-04T09:05:45.613 回答
0

不幸的是,这是当时唯一的 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;
于 2011-09-27T13:25:54.237 回答