Gdc layout renderer

Vadim edited this page Jan 25, 2018 · 9 revisions

Global Diagnostics Context - a dictionary structure to hold per-application-instance values.

Use the Global Diagnostics Context when you want to make certain information available to every logger in the current process.

The global context is one of the context.

  • When you need the context for all threads, use the GDC.
  • When you need the context for one thread, use the MDC.
  • When you work with async, use the MDLC.
  • If the context is different for every message, use the Log event properties.

As of NLog 4.1, the Global Diagnostics Context supports any Object type, not just String.

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

Configuration Syntax



Rendering Options

  • item - Name of the item. Required.


Simple Properties

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

GlobalDiagnosticsContext.Set("informationGroup", new { One = 1, Two = 2 });
GlobalDiagnosticsContext.Set("anyObject", anyObjectReferenceOrString);

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


Dynamic Properties

In some instances you may have thread-local information that you want to make available to all logger instances in the current process. This can be achieved with the Mapped Diagnostics Context, but requires that your create the property within the context of every thread that may reference it. Using Dynamic Properties and the Global Diagnostics Context, you can achieve the same result while only creating the property once.

public class ManagedThreadIdProperty
   public static readonly ManagedThreadIdProperty Default = new ManagedThreadIdProperty();

   private ManagedThreadIdProperty () 

   public override string ToString ()
      return System.Threading.Thread.CurrentThread.ManagedThreadId.ToString();

During initialization, add the following code:

GlobalDiagnosticsContext.Set("ManagedThreadId", ManagedThreadIdProperty.Default);

To reference the ManagedThreadId Global Diagnostics Context property.



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

Clone this wiki locally
You can’t perform that action at this time.
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.
Press h to open a hovercard with more details.