Logging not working in Class Library project

3 messages Options
Embed this post
Permalink
jose

Logging not working in Class Library project

Reply Threaded More More options
Print post
Permalink
Hi,

I have a Class Library project in VB.NET but I can't get NLog writting logs to a file. Same configuration works ok for a Console application. Additional config for Class Library is required?

The NLog config file is:

<?xml version="1.0" encoding="utf-8" ?>
<!--
  This file needs to be put in the application directory. Make sure to set
  'Copy to Output Directory' option in Visual Studio.
  -->
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

    <targets>
        <target name="file" xsi:type="File" fileName="${basedir}/log.txt"
                layout="${longdate}|${level:uppercase=true}|${logger}|${message}" />
    </targets>

    <rules>
        <logger name="*" minlevel="Debug" writeTo="file" />
    </rules>
</nlog>

The file has the option "Copy to output directory" with "Copy always" value.

The use of the library is as:

  Private Shared logger As Logger = LogManager.GetCurrentClassLogger()
  ...
  Public Sub ExecuteAction()
            logger.Debug("ExecuteAction")
            ...
 End Sub


Thanks in advance,
Jose
Jaroslaw Kowalski

Re: Logging not working in Class Library project

Reply Threaded More More options
Print post
Permalink
You need to make sure NLog.config ends up being copied to the application output directory (the same location where your *.exe file is). I'm not sure if this is any different in VB, but C# should handle this correctly even from the library project.
 
Jarek

On Thu, Sep 24, 2009 at 1:18 PM, jose (via Nabble) - No Reply <[hidden email]> wrote:
Hi,

I have a Class Library project in VB.NET but I can't get NLog writting logs to a file. Same configuration works ok for a Console application. Additional config for Class Library is required?

The NLog config file is:

<?xml version="1.0" encoding="utf-8" ?>
<!--
  This file needs to be put in the application directory. Make sure to set
  'Copy to Output Directory' option in Visual Studio.
  -->
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

    <targets>
        <target name="file" xsi:type="File" fileName="${basedir}/log.txt"
                layout="${longdate}|${level:uppercase=true}|${logger}|${message}" />
    </targets>

    <rules>
        <logger name="*" minlevel="Debug" writeTo="file" />
    </rules>
</nlog>

The file has the option "Copy to output directory" with "Copy always" value.

The use of the library is as:

  Private Shared logger As Logger = LogManager.GetCurrentClassLogger()
  ...
  Public Sub ExecuteAction()
            logger.Debug("ExecuteAction")
            ...
 End Sub


Thanks in advance,
Jose

jose

Re: Logging not working in Class Library project

Reply Threaded More More options
Print post
Permalink
Hi Jarek

Thanks for the response.

The project is a DLL, not an EXE. NLog.config is copied in the output directory of the dll.

I just discovered the problem, the log file is created in the directory of the application that is using the DLL, not in the directory where resides the DLL.

I'll check how to change in code the location of the log file.

Regards,
Jose