I have WPF app and i config Nlog from code:
NlogConfig.cs
public class NlogConfig
{
private static readonly NLog.Logger Logger = NLog.LogManager.GetCurrentClassLogger();
public static void Configure(string path, LogLevel minLevel, LogLevel maxLevel)
{
var layout = @"${date:format=yyyy-MM-dd HH\:mm\:ss} ${level:padding=-5:fixedlength=true} | ${callsite:padding=30:fixedlength=true:inner=Layout:alignmentOnTruncation=right} | ${message}";
var logfile = new NLog.Targets.FileTarget("logfile") { FileName = path };
logfile.ArchiveAboveSize = 20_971_520;
logfile.MaxArchiveFiles = 1;
logfile.Layout = layout;
var traceconsole = new NLog.Targets.TraceTarget("traceconsole");
traceconsole.Layout = layout;
// Rules for mapping loggers to targets
var config = new NLog.Config.LoggingConfiguration();
config.AddRule(minLevel, maxLevel, traceconsole);
config.AddRule(minLevel, maxLevel, logfile);
// Apply config
LogManager.Configuration = config;
Logger.Debug("Nlog configured");
}
When I log message using Debug I get output like this:
2023-10-23 16:06:22 Debug | iguration.NlogConfig.Configure | Nlog configured
But when I use Info I get such output:
test.exe Information: 0 : 2023-10-23 16:08:44 Info | iguration.NlogConfig.Configure | Nlog configured
What I did wrong why the output for Debug and Info different? I want to use the same layout for all log levels.
UPD:
In log.txt everything fine. But when I switch NLog.Targets.TraceTarget to NLog.Targets.ConsoleTarget the log messages in console is right.
Try to enable RawWrite = true
for TraceTarget
:
var traceconsole = new NLog.Targets.TraceTarget("traceconsole");
traceconsole.RawWrite = true;
traceconsole.Layout = layout;
Layout Options
rawWrite - Always use
Trace.WriteLine
independent of LogLevel.Default: False
Introduced with NLog 4.5, fixes the issue with output being prefixed with exe-filename.