A RetroSearch Logo

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

Search Query:

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

Gdc layout renderer · NLog/NLog Wiki · GitHub

Global Diagnostic Context (GDC) - Dictionary structure for global settings for the entire application-instance.

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

GDC properties should be assigned at application-startup and remain static and immutable during the lifetime of the application/process. Do NOT modify the GDC as part of the logging operations, as it will have unwanted side-effects. If needing to include additional context information for a specific logevent, then consider using Logger.WithProperty() or Logger.PushScopeProperty().

See also NLog Context.

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

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

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

${gdc:item=myDatabase}
${gdc:item=informationGroup}
${gdc:item=anyObject}

The Global Diagnostics Context (GDC) can also store complex objects, and can be used as an alternative to registering own custom layout renderer. NLog sees the GDC like global variables that doesn't depend on thread execution context, so make sure any complex object logic doesn't depend on application logging thread-state.

public class ApplicationStateProperty : IFormattable
{
   public static readonly ApplicationStateProperty Default = new ApplicationStateProperty();

   private ApplicationStateProperty() 
   {
   }

   public override string ToString()
   {
      // Physical memory used by application process (in megabytes)
      return ((long)(System.Environment.WorkingSet / (1024 * 1024))).ToString();
   }

   string IFormattable.ToString(string format, IFormatProvider formatProvider)
   {
      return ToString();
   }
}

During initialization, add the following code:

GlobalDiagnosticsContext.Set("AppMemorySize", ApplicationStateProperty.Default);

To reference the AppMemorySize Global Diagnostics Context property.

${gdc:item=AppMemorySize}

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