NLog with VS 2008 Unit Test

拜拜、爱过 提交于 2019-12-23 10:59:35

问题


I am trying log some entries in a log file (Log.Trace / Log.Debug) while my VS unit test runs. I have also copied the file NLog.config to out directory through DeploymentItem attribute over class. Still my log file is not created. Any help as to how can we log entries in a file same as we do for normal web application.


回答1:


I've just found a solution to this problem: Add the App.config file to yout unit test project with the following contents:

<?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>

You may put any configuration you wish as with NLog.config file.




回答2:


Unfortunately that is the only XML solution at the time. It is not only about Unit Testing, though. NLog.config does not work with just any Console Application.

Do not know if it is by design or just an oversight. Anyway I would not say that moving NLog.config to App.config section is any way satisfactory =/

Maybe it is worth to notice that there is a possibility of configuring nlog directly from code, what could be helpful in some scenarios. One could be also glad to find nlog debug option, that will log the whole process of processing configuration file, registering targets and so on...

To turn it on, just add internalLogFile and internalLogLevel attributes to nlog element:

<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">

or from code:

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

    InternalLogger.LogLevel = LogLevel.Trace;


来源:https://stackoverflow.com/questions/1672998/nlog-with-vs-2008-unit-test

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!