Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
/NLogPublic

Gdc layout renderer

Rolf Kristensen edited this pageNov 15, 2025 ·27 revisions

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

Platforms Supported:All (NLog 4.1 allows storing anyObject type, not justString)

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 usingLogger.WithProperty() orLogger.PushScopeProperty(). See alsoNLog Context.

NLog also supports alternative locations for global state likeEnvironment Variables,AppSettings.json, etc. See alsoEnvironment-specific-NLog-Logging-Configuration

Configuration Syntax

${gdc:item=String}

Parameters

Rendering Options

  • item - Name of the item. Required.Note Before NLog 5.0 it was case-sensitive.
  • culture - The culture used for rendering. Introduced in NLog 5.0. Default value isCultureInfo.InvariantCulture
  • format - Format for conversion from object to string. Introduced in NLog 4.5.
    • format=@ means serialize object properties into Json-format.

Example

Simple Properties

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}

Dynamic Properties

The Global Diagnostics Context (GDC) can also store complex objects, and can be used as an alternative to registering owncustom 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.

publicclassApplicationStateProperty:IFormattable{publicstaticreadonlyApplicationStatePropertyDefault=newApplicationStateProperty();privateApplicationStateProperty(){}publicoverridestringToString(){// Physical memory used by application process (in megabytes)return((long)(System.Environment.WorkingSet/(1024*1024))).ToString();}stringIFormattable.ToString(stringformat,IFormatProviderformatProvider){returnToString();}}

During initialization, add the following code:

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

To reference theAppMemorySize Global Diagnostics Context property.

${gdc:item=AppMemorySize}

Notes

When rendering context items, the item is passed toString.Format along with the current configuration'sDefaultCultureInfo value.

-Troubleshooting Guide - See available NLog Targets and Layouts:https://nlog-project.org/config

Configuration

Programmatic Configuration

Advanced

Extending NLog

Clone this wiki locally


[8]ページ先頭

©2009-2025 Movatter.jp