Layout Renderers

Wrapper Layout Renderers

Custom Layout Renderers

Passing Custom Values to a Layout

Even though the layout renderers provide many pre-defined values, you may need to pass application specific values to yourLayouts. You can pass your own values in code by adding custom Properties to the event. You then retrieve the value using the${event-context} renderer. See the documentation for the ${event-context} for an example.

 

Log event context data.

Supported in .NET, Silverlight, Compact Framework and Mono.

Configuration Syntax

${event-context:item=String}

Parameters

Rendering Options

  • item - Name of the item. Required.

Example

In C# class, create an event and add an element to the Properties dictionary (or the deprecated Context dictionary):

...
Logger log = LogManager.GetCurrentClassLogger();
LogEventInfo theEvent = new LogEventInfo(LogLevel.Debug, "", "Pass my custom value");
theEvent.Properties["MyValue"] = "My custom string";
// deprecated
theEvent.Context["TheAnswer"] = 42;
log.Log(theEvent);
...

and in your NLog.config file:

${event-context:item=MyValue} -- renders "My custom string"
${event-context:item=TheAnswer} -- renders "42"