Skip to content

NDC Layout Renderer

Rolf Kristensen edited this page Aug 29, 2023 · 16 revisions

⚠️ NLog 5.0 introduces ScopeContext that replaces NDLC + NDC

Nested Diagnostics Context - a thread-local structure that keeps a stack of strings and provides methods to output them in layouts.

Platforms Supported: All

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

Configuration Syntax

${ndc:bottomFrames=Integer:topFrames=Integer:separator=String}

Parameters

Rendering Options

  • bottomFrames - Number of bottom stack frames to be rendered. -1 is no limit. Integer. Default -1.
  • topFrames - Number of top stack frames to be rendered. -1 is no limit. Integer. Default -1
  • separator - Separator to be used for concatenating nested diagnostics context output. string. Default (space)

Examples

One level

NestedDiagnosticsContext.Push("entering method X");
... // log here
NestedDiagnosticsContext.Pop(); //leaving methods

Then in the log config:

${ndc}

logs entering method X if logged between push() and pop()

Multiple levels

NestedDiagnosticsContext.Push("entering method X1");
NestedDiagnosticsContext.Push("entering method X2");
... // log here
NestedDiagnosticsContext.Pop(); //leaving method X2
NestedDiagnosticsContext.Pop(); //leaving method X1

logs entering method X1 entering method X2

Clone this wiki locally