NLog 全局属性
NLog global properties
使用 NLog 有什么方法可以定义日志的全局 LogEventInfo
属性吗?
使用构造
var logEventInfo = new LogEventInfo(LogLevel.Trace, Log.Name, "Hello world");
logEventInfo.Properties["messageName"] = "MessageName2";
不适合,因为在大项目中到处都在使用Info(...)
和类似的方法。但我们需要添加一些参数以在 Gelf
日志中观察它们而无需重写代码。
您是否在寻找全局诊断上下文/映射诊断上下文?
在您的代码中设置:
GlobalDiagnosticsContext.Set("prop1", "I'm scoped for all threads");
MappedDiagnosticsContext.Set("prop2", "I'm scoped per thread and child thread");
配置中的用法:
${gdc:item=prop1}
${mdc:item=prop2}
另见 https://github.com/nlog/nlog/wiki/gdc-Layout-Renderer & https://github.com/nlog/nlog/wiki/mdc-Layout-Renderer
使用 NLog 有什么方法可以定义日志的全局 LogEventInfo
属性吗?
使用构造
var logEventInfo = new LogEventInfo(LogLevel.Trace, Log.Name, "Hello world");
logEventInfo.Properties["messageName"] = "MessageName2";
不适合,因为在大项目中到处都在使用Info(...)
和类似的方法。但我们需要添加一些参数以在 Gelf
日志中观察它们而无需重写代码。
您是否在寻找全局诊断上下文/映射诊断上下文?
在您的代码中设置:
GlobalDiagnosticsContext.Set("prop1", "I'm scoped for all threads");
MappedDiagnosticsContext.Set("prop2", "I'm scoped per thread and child thread");
配置中的用法:
${gdc:item=prop1}
${mdc:item=prop2}
另见 https://github.com/nlog/nlog/wiki/gdc-Layout-Renderer & https://github.com/nlog/nlog/wiki/mdc-Layout-Renderer