wpf 当LogLevel Info时,Nlog TraceTarget输出exe文件

cdmah0mi  于 7个月前  发布在  其他
关注(0)|答案(1)|浏览(74)

我有WPF应用程序,我从代码中配置Nlog:
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");
}

字符串
当我使用MySQL记录消息时,我会得到这样的输出:
第一个月
但是当我使用Info时,我得到这样的输出:
test.exe Information: 0 : 2023-10-23 16:08:44 Info | iguration.NlogConfig.Configure | Nlog configured
我做错了什么为什么输出为和信息不同?我想使用相同的布局为所有日志级别。
UPD:
在log.txt中一切正常。但是当我将NLog.Targets.TraceTarget切换到NLog.Targets.ConsoleTarget时,控制台中的日志消息是正确的。

68de4m5k

68de4m5k1#

尝试为TraceTarget启用RawWrite = true

var traceconsole = new NLog.Targets.TraceTarget("traceconsole");
    traceconsole.RawWrite = true;
    traceconsole.Layout = layout;

字符串

布局选项
rawWrite-始终使用独立于LogLevel的Trace.WriteLineDefault: False

在NLog 4.5中引入,修复了输出以exe-filename为前缀的问题。
标签:https://github.com/NLog/NLog/wiki/Trace-target

相关问题