【问题标题】:NLog Log writing to different after changing filename of a targetNLog 更改目标文件名后写入不同的日志
【发布时间】:2020-02-11 12:01:17
【问题描述】:

我已经提到了这个 Update NLog target filename at runtime 和许多其他链接,但在我的情况下似乎没有一个有效。 我使用

初始化记录器
private Logger logger = LogManager.GetCurrentClassLogger();

所有日志消息都排队,然后使用计时器清空。

this.messageQueue = new ConcurrentQueue<NotificationMessagePacket>();
this.timer = new System.Timers.Timer(1000);
this.timer.Elapsed += (o, e) =>
{
    if (!isWriting)
    {
        lock (_lockObject)
        {
            isWriting = true;
            NotificationMessagePacket packet;
            while (this.messageQueue.TryDequeue(out packet) && packet != null)
            {
                try
                {
                    if (!string.IsNullOrWhiteSpace(packet.DetailMessage))
                        this.Infolog(packet);
                }
                catch (Exception ex)
                {
                    ObjectUtils.EventLogWriteError("NotificationMessagePacket emptying error : " + ex.ToString(),
                        EventLogEntryType.Warning);
                }
            }
        }
        isWriting = false;
    }
};
this.timer.Start();

我正在尝试使用以下代码重置目标的文件名:

LoggingConfiguration configuration = LogManager.Configuration;

var wrapper = (AsyncTargetWrapper)configuration.FindTargetByName("log");

var target = (FileTarget)wrapper.WrappedTarget;

string path = Path.Combine(basePath, "Test", "Log", string.Concat(DateTime.Now.ToString("dd"), "_", DateTime.Now.ToString("MMMM"), "_", DateTime.Now.Year + @"\AppLogs.txt"));

if (!string.IsNullOrEmpty(message.ProcessId))
    path = Path.Combine(basePath, "Test", "Log", string.Concat(DateTime.Now.ToString("dd"), "_", DateTime.Now.ToString("MMMM"), "_", DateTime.Now.Year + @"\" + message.ProcessId + ".txt"));

target.FileName = path;
target.ConcurrentWrites = false;
LogManager.Configuration = configuration;
LogManager.ReconfigExistingLoggers();

但有时,日志消息不会写入适当的文件。

【问题讨论】:

  • 我会推荐使用 GDC,看到这个答案:stackoverflow.com/a/53145100/201303
  • 您可能需要的不仅仅是 GDC,请参阅(长)答案。希望它能解决您的问题!
  • 嗨 Julian,感谢解决方案有效

标签: c# nlog


【解决方案1】:

问题(可能)

我在你的配置中看到了message.ProcessId。看起来您正在更改每个日志事件的配置。那不是线程安全的!见How Thread-Safe is NLog?

如何解决

好消息,也不需要更改配置。您可以使用上下文类在配置中注入值。对于全局,有 GDC,但也有线程和消息级别的上下文。 (和其他人here)。

示例如何修复

你可以这样设置配置:

// Set GDC
GlobalDiagnosticsContext.Set("basepath","somepath");

// Create config
var config = new LoggingConfiguration();

FileTarget fileTarget1 = new FileTarget();
fileTarget1.FileName = @"${gdc:basepath}\Test\Log\${Date:format:dd_MMMM_yyyy}\AppLogs.txt";

FileTarget fileTargetWithProcessId = new FileTarget();
fileTargetWithProcessId.FileName = @"${gdc:basepath}\Test\Log\${Date:format:dd_MMMM_yyyy}\${event-properties:ProcessId}.txt";

var hasProcessIdProperty = "${event-properties:item=ProcessId}!=''";
var rule1 = new LoggingRule()
{ 
    // log without processid to fileTarget1
    Targets = { fileTarget1 },
    Filters = { new ConditionBasedFilter()
    {
        Condition = hasProcessIdProperty,
        Action = FilterResult.Ignore,
        DefaultFilterResult = FilterResult.Log
    } }
};
var rule2 = new LoggingRule()
{
    // log only with processid to fileTarget1
    Targets = { fileTargetWithProcessId },
    Filters = { new ConditionBasedFilter()
    {
        // Log with property processid
        Condition = hasProcessIdProperty,
        Action = FilterResult.Log,
        DefaultFilterResult = FilterResult.Ignore
    } }
};

// Enable trace to fatal (so all levels)
rule1.SetLoggingLevels(LogLevel.Trace, LogLevel.Fatal);
rule2.SetLoggingLevels(LogLevel.Trace, LogLevel.Fatal);

config.LoggingRules.Add(rule1);
config.LoggingRules.Add(rule2);

LogManager.Configuration = config; // apply

并像这样记录:

var logger = LogManager.GetCurrentClassLogger();

logger.Info("I go to AppLogs.txt");

logger.WithProperty("ProcessId", message.ProcessId).Info("I go to processid logs");

您也可以使用一个目标并使用“if else”(${when}),设置起来有点复杂。

疑难解答

如果您对此有任何问题,请查看How to see NLog diagnostics and errors

【讨论】:

    猜你喜欢
    • 2021-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多