将结构化日志记录与 NLog 一起使用时如何包含其他属性?

How to include additional properties when using structured logging with NLog?

我知道你能做到:

_Logger.Info("This is my message: {MessageId}.  And here's an explanation: {expl}", messageId, expl)

这将根据它们在字符串中的顺序将 messageId 和 expl 的值插入到消息中,如 String.Format。

不幸的是,我想添加结构化消息记录之外的额外属性,所以我使用 LogEventInfo:

Dim ev as New LogEventInfo(LogLevel.Info, "", "This is my message: {MessageId}")
ev.Properties{"EventId"}=eventId
_Logger.Log(ev)

是否有其他方式以优雅的方式添加额外的属性?

我认为您正在寻找 WithProperty 添加邮件中没有的属性。

示例:

_Logger.WithProperty("EventId", eventId)
       .Info("This is my message: {MessageId}.  And here's an explanation: {expl}", messageId, expl)

另请参阅:https://github.com/NLog/NLog/wiki/Context#logevent-properties