【问题标题】:Structured logging with Common.Logging and NLog使用 Common.Logging 和 NLog 进行结构化日志记录
【发布时间】:2019-01-21 14:58:50
【问题描述】:

我的项目中有最新版本的Common.Logging (3.4.1) 和NLog (4.5.11)。按照说明在 app.config 中将 Common.Logging 与 NLog 绑定在一起,现在可以进行日志记录了。

现在我想要结构化日志记录:

private static readonly ILog _logger = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

var message = new IDontKnowWhat("A structured message for ClientId {ClientId}", ClientId.ToString());
_logger.Info(message);

以上是伪代码,不知道如何构造消息对象。 而且,_logger.InfoFormat 似乎适用于 String.Format,而不是结构化日志记录。

除了使用ThreadVariablesContext/GlobalVariablesContext/NestedThreadVariablesContext之外,Common.Logging 是否提供结构化日志记录?

【问题讨论】:

  • 认为您正在等待这个问题:github.com/net-commons/common-logging/issues/159(遗憾的是,修复这将是一个“破坏性”更改,因为 Common-Logging 出于某种原因尝试执行解析和格式化,而不是让日志框架完成这项工作)。 SerilogLogger 执行 hack 以解决 Common-Logging-Framework 的限制,因此可以使用结构化日志记录。
  • 看起来确实如此.. 我现在正在尝试使用 LibLog。我已经使用 LibLog/NLog 组合进行结构化日志记录,只是在显示通过 OpenMappedContext 传入的信息时出现问题。

标签: c# nlog common.logging structured-logging


【解决方案1】:

幸运的是,Common.Logging 开发团队目前正在解决这个问题: Common.Logging.NLog45 with support for structured logging #176

【讨论】:

    【解决方案2】:

    结帐serilog 以获得结构化日志记录。如果你的需求是使用 NLog,serilog 也有一个 NLog 的接收器。

    【讨论】:

    • 我的要求是使用 Common.Logging 和结构化日志记录。如果有任何使用 Serilog 的 Common.Logging 的好例子,我可以试试。当我可以让它与 NLog 一起工作时,我会更高兴,所以我不必重新配置我所有的应用程序域。
    • 如果有帮助,有示例 Common.Logging + Serilog 信息在:github.com/CaptiveAire/Common.Logging.Serilog#readme
    猜你喜欢
    • 1970-01-01
    • 2018-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多