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.
In order to see the log output on the console you have to:
NLog.config
)<?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...
The basedir is relative to the .exe
In this case it would likely be in bin\debug\assets\logging\log.txt
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With