NLog does not display anything?

for some strange reason, nlog doesn't display anything when I write to the console in the Main () method in Program.cs. I assign 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 an example 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 that the method is called because I get "LOL" and not the actual log in the console, but it writes the file / assets / logging / log.txt.

+5
source share
4 answers

To see the log output on the console, you must add a target and a rule for it, for example:

<?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="logfile" xsi:type="File" fileName="file.txt" />
        <target name="console" xsi:type="Console" />
    </targets>

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

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

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 can use 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...
+5
source

.exe

, , bin\debug\assets\logging\log.txt

+2

. , , : Nlog git .

, ruleslogger (In writeTo File)

+2

, , , , ,

NLog IIS

" " " "

0

Source: https://habr.com/ru/post/1670853/


All Articles