使用 Common.Logging 和 NLog 进行结构化日志记录

Structured logging with Common.Logging and NLog

我的项目中有最新版本的 Common.Logging (3.4.1) and NLog (4.5.11)。按照说明将 Common.Logging 与 app.config 中的 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 是否提供结构化日志记录?

检查 serilog 以获得结构化日志记录。如果您的要求是使用 NLog,serilog 也有一个到 NLog 的接收器。

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