Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Unit testing 非直瞄与VS 2008单元测试_Unit Testing_File_Nlog - Fatal编程技术网

Unit testing 非直瞄与VS 2008单元测试

Unit testing 非直瞄与VS 2008单元测试,unit-testing,file,nlog,Unit Testing,File,Nlog,在VS单元测试运行时,我正在尝试将一些条目记录到日志文件(log.Trace/log.Debug)中。我还通过类上的DeploymentItem属性将文件NLog.config复制到out目录。仍然没有创建我的日志文件。关于如何将条目记录到与普通web应用程序相同的文件中的任何帮助。我刚刚找到了解决此问题的方法: 将App.config文件添加到yout单元测试项目中,内容如下: <?xml version="1.0" encoding="utf-8" ?> <configur

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

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

<?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文件中。

不幸的是,这是当时唯一的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;