Skip to content

MDLC Layout Renderer

Julian Verdurmen edited this page Apr 14, 2019 · 12 revisions

Use the Mapped Diagnostic Logical Context when you have information that you want available to every logger executing on the current logical context (Similar to "Thread Context Map" in Log4j)

Mapped Diagnostic Logical Context is the async version of the Mapped Diagnostics Context - a logical call context structure that keeps a dictionary of strings and provides methods to output them in layouts. Allows for maintaining state across asynchronous tasks and call contexts.

See also LogEvent Context Information.

  • As of NLog 4.1 in the NLog package.
  • As of NLog 4.1.1, the Mapped Diagnostics Context supports any Object type, not just String.

Configuration Syntax

${mdlc:item=String}

Parameters

Rendering Options

  • item - Name of the item. Required.

Example

Simple Properties

The following example demonstrates the basic usage of the Mapped Diagnostic Logical Context.

MappedDiagnosticsLogicalContext.Set("PropertyName", "PropertyValue");
MappedDiagnosticsLogicalContext.Set("PropertyName2", "AnotherPropertyValue");

Add the following to your logger configuration to reference the above properties.

${mdlc:item=PropertyName}
${mdlc:item=PropertyName2}

Scoped item

The SetScoped method returns an IDisposable that removes the added item when disposed. It can be used in conjunction with the using statement to limit the scope during which the item will be present in the context.

using (MappedDiagnosticsLogicalContext.SetScoped("Property", "PropertyValue")) {
    // "Property" item is present in current context
}
// "Property" item has been removed from current context
Clone this wiki locally
You can’t perform that action at this time.