Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NLog: Where is NLog Config?

Tags:

c#

nlog

I coded this:

private static readonly ILogger Logger = LogManager.GetCurrentClassLogger();

public MyClass()
{
    Console.Write("lol");
    Logger.Debug("Debug test...");
    Logger.Error("Debug test...");
    Logger.Fatal("Debug test...");
    Logger.Info("Debug test...");
    Logger.Trace("Debug test...");
    Logger.Warn("Debug test...");
}

And nothing displays.. so I was told to go and add <targets> to the config file, thing is.. where is the config file? Nothing on google, the documentation, or anything like that helps me...

like image 728
Ashkru Avatar asked Sep 20 '25 16:09

Ashkru


2 Answers

From NLog Wiki:

The following locations will be searched when executing a stand-alone *.exe application:

  • standard application configuration file (usually applicationname.exe.config)
  • applicationname.exe.nlog in application’s directory
  • NLog.config in application’s directory
  • NLog.dll.nlog in a directory where NLog.dll is located (only if NLog isn't installed in the GAC)

So, the easiest would be to add a NLog.config file in the application’s directory

like image 88
Ofir Winegarten Avatar answered Sep 22 '25 08:09

Ofir Winegarten


Sample Nlog.config File for your reference:

<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      throwConfigExceptions="true">

    <targets>
        <target name="logfile" xsi:type="File" fileName="file.txt" />
        <target name="logconsole" xsi:type="Console" />
    </targets>

    <rules>
        <logger name="*" minlevel="Info" writeTo="logconsole" />
        <logger name="*" minlevel="Debug" writeTo="logfile" />
    </rules>
</nlog>

See also: https://github.com/NLog/NLog/wiki/Tutorial

like image 25
Joe Avatar answered Sep 22 '25 08:09

Joe