A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/NLog/NLog/wiki/MDC-Layout-Renderer below:

MDC Layout Renderer · NLog/NLog Wiki · GitHub

⚠️ NLog 5.0 introduces ScopeContext that replaces MDLC + MDC

Mapped Diagnostics Context (MDC) - a thread-local structure that keeps a dictionary of strings and provides methods to output them in layouts.

Platforms Supported: All (NLog 4.1 supports any Object type, not just String)

MDC is considered legacy, and instead is recommended to use MDLC that includes support for async Tasks. See also NLog Context

The following example demonstrates the basic usage of the Mapped Diagnostics Context.

MappedDiagnosticsContext.Set("PropertyName", "PropertyValue");
MappedDiagnosticsContext.Set("Property2", new { Part1 = 2.0m, Part2 = "Two parts" });
MappedDiagnosticsContext.Set("Property3", AnyObjectOrString);

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

${mdc:item=PropertyName}
${mdc:item=Property2}
${mdc:item=Property3}

The following example demonstrates a Mapped Diagnostics Context property that renders the value of Environment.TickCount at the time that the context item is rendered.

public class MdcTickProperty 
{
   public static readonly MdcTickProperty Default = new MdcTickProperty();

   private MdcTickProperty () 
   {
   }

   public override string ToString () 
   {
      return Environment.TickCount.ToString();
   }
}

Add the MdcTickProperty instance to the Mapped Diagnostics Context. This will only affect the current thread, as the Mapped Diagnostics Context is thread-local.

MappedDiagnosticsContext.Set("TickCount", MdcTickProperty.Default);

In the logging configuration, include:

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 (MappedDiagnosticsContext.SetScoped("Property", "PropertyValue")) {
    // "Property" item is present in current context
}
// "Property" item has been removed from current context

When rendering context items, the item is passed to String.Format along with the current configuration's DefaultCultureInfo value.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4