Степан Степан - 1 month ago 28
C# Question

NLog global properties

Using NLog is there any way to define global

LogEventInfo
properties for logs?
Using construction

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


isn't suitable, because in big project everywhere using
Info(...)
and similar methods. But we need to add some parameters to watch them at
Gelf
logging without rewriting code.

Answer

Are you looking for the global diagnostic context / mapped diagnostic context?

Set in your code:

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

Usage in config:

${gdc:item=prop1}
${mdc:item=prop2}

See also https://github.com/nlog/nlog/wiki/gdc-Layout-Renderer & https://github.com/nlog/nlog/wiki/mdc-Layout-Renderer

Comments