You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Introduced in NLog 4.6.3, where Logger have the method WithProperty.
The Logger can be enriched, so it automatically injects one (or more) log-event properties for all log-events being written by the Logger. This can work as an alternative to ScopeContext properties.
Examples:
// WithProperty will return a new unique Logger with the newly added propertyvarnewLogger=logger.WithProperty("myProperty",myValue);newLogger.Info("hello");newLogger.Info("again");// will also have "myProperty"logger.Info("hi");// is not affected
_logger.ForInfoEvent().Message("This is a fluent message {0}.","test").Property("PropertyName","PropertyValue").Log();
Fallback to default value
When LogEvent Property cannot be found (or has blank value), then one can use whenEmpty to specify fallback value:
${event-properties:EventId:whenEmpty=42}
Objectpath
Examples with usage of Objectpath. NLog 4.6.3+
Set the property:
varproperty1=new{Id=1,Name="test",Nested=new{Id=3}};varlogger=LogManager.GetLogger("logger1");// e.g. with WithPropertylogger.WithProperty("prop1",property1).Info("test message");// or with structured logginglogger.Info("test Message with {prop1}",property1);
config examples:
${event-properties:prop1} -- renders: { Id = 1, Name = test, Nested = { Id = 3 } }
${event-properties:prop1:objectpath=Id} -- renders: 1
${event-properties:prop1:objectpath=Nested.Id} -- renders: 3