NLog 中属性字典和变量字典的区别

Difference between Properties dictionary and Variables dictionary in NLog

有了 NLog,我可以添加一个 属性:

eventInfo = new LogEventInfo();
eventInfo.Properties.Add(name, value);

和一个变量:

loggerInstance = LogManager.GetLogger(typeName);
loggerInstance?.Factory.Configuration.Variables.Add(name, value);

然后我问自己:有什么区别?环顾四周,但没有找到明确的解释,只是警告 gdc、mdc 和 mdlc 已过时。我应该将哪一个用于什么目的 - 我可能需要两者(或两者都不需要。)

what is the difference

  • eventInfo.Properties:一个logEvent范围内的context。您可以使用 ${event-properties:myPropName}
  • 渲染它们
  • Configuration.Variables:在 nlog.config 和 <variable /> 中使用的变量。您可以在配置中使用它们:
    • ${var:myVar}- 动态,但仅适用于 Layout 类型的属性
    • ${myVar} - 对于所有属性,将在解析 nlog.config
    • 时应用

gdc, mdc, and mdlc are obsolete

类 GDC、MDC 和 MDLC 已过时,但 不是 GlobalDiagnosticsContext MappedDiagnosticsContextMappedDiagnosticsLogicalContext

这些是线程安全上下文:

  • GlobalDiagnosticsContext:完整申请。使用 ${gdc:myKey}
  • 渲染
  • MappedDiagnosticsContext:当前线程,但是由该线程创建的线程。使用 ${mdc:myKey}
  • 渲染
  • MappedDiagnosticsLogicalContext:当前线程包括本线程创建的线程。使用 ${mdlc:myKey}.
  • 渲染

有关详细信息,请参阅 https://nlog-project.org/config/?tab=layout-renderers&search=context