Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NLog not displaying anything?

Tags:

c#

nlog

for some weird reason nlog is not displaying anything when I write to the console, in the Main() method in Program.cs I assign the nlog.config:

LogManager.Configuration = new XmlLoggingConfiguration("assets/packages/nlog/nlog.config");

Here is the Config:

<nlog throwExceptions="true">
  <targets>
    <target name="file" type="File" fileName="${basedir}/assets/logging/log.txt" />
  </targets>
  <rules>
    <logger name="*" minLevel="Info" writeTo="File" />
  </rules>
</nlog>

Here is a sample class:

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...");
}

I know the method is being called because I get "lol", just not the actual log on the console, but it does write to /assets/logging/log.txt file.

like image 725
Ashkru Avatar asked Feb 26 '17 05:02

Ashkru


2 Answers

In order to see the log output on the console you have to:

  • Have a configuration file (Ex: NLog.config)
  • Ensure that the configuration file is copied to the output directory on build
  • Edit it to add a target and a rule for it:
<?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">

    <targets>
        <target name="console" xsi:type="Console" />
    </targets>

    <rules>
        <logger name="*" minlevel="Info" writeTo="console" />
    </rules>
</nlog>

By creating a simple Logger class your example worked for me (using the above config file)

class MyLoggerClass
{
    public static Logger Logger = LogManager.GetCurrentClassLogger();
}

class MyClass
{
    static void Main(string[] args)
    {

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

Or you could use the Logger directly in your class like this:

class MyClass
{
    private static Logger Logger = LogManager.GetCurrentClassLogger();
    static void Main(string[] args)
    {
        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...");
    }
}

Output from log.txt:

2017-02-26 16:13:44.8388|ERROR|NLogTest.Program|Debug test...
2017-02-26 16:13:44.8856|FATAL|NLogTest.Program|Debug test...
2017-02-26 16:13:44.8856|INFO|NLogTest.Program|Debug test...
2017-02-26 16:13:44.8971|WARN|NLogTest.Program|Debug test...
like image 141
Dave S Avatar answered Sep 28 '22 04:09

Dave S


The basedir is relative to the .exe

In this case it would likely be in bin\debug\assets\logging\log.txt

like image 34
wdavo Avatar answered Sep 28 '22 02:09

wdavo