2009-11-04 65 views
5

我尝试在我的VS单元测试运行时在日志文件(Log.Trace/Log.Debug)中记录一些条目。我还通过类的DeploymentItem属性将文件NLog.config复制到了目录中。我的日志文件还没有创建。对于我们如何才能在文件中记录与正常Web应用程序相同的文件有帮助。NLog与VS 2008单元测试

回答

11

我刚刚找到了解决这个问题: 添加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任何配置。配置文件。

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;