Ashkru
Ashkru

Reputation: 1635

NLog not displaying anything?

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.

Upvotes: 6

Views: 10606

Answers (4)

Dave S
Dave S

Reputation: 1009

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

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

Upvotes: 8

Clyde
Clyde

Reputation: 742

If this is still a problem for you please check this out, I copied and pasted all the necessary code and config but didnt work out until I did this one

NLog doen't work on IIS

"Copy to Output Directory" set as "Copy always"

Upvotes: 1

Ori Nachum
Ori Nachum

Reputation: 598

You need to send the data to Console as well. Add another target that sends to console as written here: Nlog Console target git hub example.

And I think you should also put that target in the ruleslogger (In writeTo along with File)

Upvotes: 2

wdavo
wdavo

Reputation: 5110

The basedir is relative to the .exe

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

Upvotes: 2

Related Questions