Ashish
Ashish

Reputation: 2564

NLog with VS 2008 Unit Test

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.

Upvotes: 5

Views: 2563

Answers (2)

mikus
mikus

Reputation: 3215

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;

Upvotes: 0

Alexey Sharayev
Alexey Sharayev

Reputation: 136

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.

Upvotes: 12

Related Questions