NLog全局属性

Сте*_*пан 2 c# parameters nlog

使用NLog是否可以定义LogEventInfo日志的全局属性?使用构造

var logEventInfo = new LogEventInfo(LogLevel.Trace, Log.Name, "Hello world");
logEventInfo.Properties["messageName"] = "MessageName2";
Run Code Online (Sandbox Code Playgroud)

不适合,因为在大型项目中到处都使用Info(...)类似的方法。但是我们需要添加一些参数以在Gelf登录时监视它们,而无需重写代码。

Jul*_*ian 6

您是否正在寻找全局诊断上下文/映射的诊断上下文?

设置您的代码:

GlobalDiagnosticsContext.Set("prop1", "I'm scoped for all threads");
MappedDiagnosticsContext.Set("prop2", "I'm scoped per thread and child thread");
Run Code Online (Sandbox Code Playgroud)

在配置中的用法:

${gdc:item=prop1}
${mdc:item=prop2}
Run Code Online (Sandbox Code Playgroud)

另请参阅https://github.com/nlog/nlog/wiki/gdc-Layout-Rendererhttps://github.com/nlog/nlog/wiki/mdc-Layout-Renderer