Reputation: 43
I started using NLog and I want to log every line into a log file and a console for debugging purposes. When I try to log every single log action is printed 2-3 times in both console and the log file, instead of one time in each target.
I configured NLog in NLog.config file:
<?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="C:\Users\user\source\repos\TaskSchedulerConsole\Log.txt" />
<target name="logconsole" xsi:type="Console" />
</targets>
<rules>
<logger name="*" minlevel="Trace" writeTo="logfile, logconsole" />
<logger name="*" minlevel="Debug" writeTo="logfile, logconsole" />
<logger name="*" minlevel="Info" writeTo="logfile, logconsole" />
<logger name="*" minlevel="Warn" writeTo="logfile, logconsole" />
<logger name="*" minlevel="Error" writeTo="logfile, logconsole" />
<logger name="*" minlevel="Fatal" writeTo="logfile, logconsole" />
</rules>
</nlog>
In every class I created a static logger:
private static readonly NLog.Logger Logger = NLog.LogManager.GetCurrentClassLogger();
Example for triple logging: Program.cs:
try
{
Logger.Info("Program Started.");
}
catch(Exception ex)
{
Logger.Info(ex, "Something went wrong...");
}
finally
{
Logger.Info("Program Finished.");
NLog.LogManager.Shutdown();
}
Output in both console and log file:
2020-05-14 11:49:44.7542|INFO|TaskSchedulerConsole.Program|Program Started.
2020-05-14 11:49:44.7542|INFO|TaskSchedulerConsole.Program|Program Started.
2020-05-14 11:49:44.7542|INFO|TaskSchedulerConsole.Program|Program Started.
2020-05-14 11:49:44.8315|INFO|TaskSchedulerConsole.Program|Program Finished.
2020-05-14 11:49:44.8315|INFO|TaskSchedulerConsole.Program|Program Finished.
2020-05-14 11:49:44.8315|INFO|TaskSchedulerConsole.Program|Program Finished.
Thanks in advance for everyone, hope someone can help me understand the problem.
Upvotes: 0
Views: 1358
Reputation: 1532
1.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"
xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd"
autoReload="true"
throwExceptions="false"
internalLogLevel="Off" internalLogFile="c:\temp\nlog-internal.log">
<variable name="myvar" value="myvalue"/>
<targets>
<target name="logfile" xsi:type="File" fileName="log-dest\Log.txt" />
<target name="logconsole" xsi:type="Console" />
</targets>
<rules>
<logger name="*" minlevel="Info" writeTo="logfile, logconsole" />
</rules>
</nlog>
2.program.cs
class Program
{
private static readonly NLog.Logger Logger = NLog.LogManager.GetCurrentClassLogger();
static void Main(string[] args)
{
try
{
Logger.Info("Program Started.");
}
catch (Exception ex)
{
Logger.Error(ex, "Something went wrong...");
}
finally
{
Logger.Info("Program Finished.");
NLog.LogManager.Shutdown();
}
}
}
Upvotes: 7
Reputation: 4037
<logger name="*" minlevel="Trace" writeTo="logfile" />
<logger name="*" minlevel="Trace" writeTo="logconsole" />
should be enough to log every level entry once
the problem is that you are adding loggers several times, like
<logger name="*" minlevel="Trace" writeTo="logfile, logconsole" />
<logger name="*" minlevel="Debug" writeTo="logfile, logconsole" />
adds two loggers: one for all events starting from the Trace
and second - for all levels starting from the Debug
level.
Upvotes: 2
Reputation: 330
It is the correct behavior.
You config six rules for your NLog.config, and three of these six rules will be triggered in your code. (Trace/Debug/Info)
Upvotes: 2