Skip to content

NDLC Layout Renderer

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

⚠️ NLog 5.0 introduces ScopeContext that replaces NDLC + NDC

Nested Diagnostics Logical Context (NDLC) acts like a stack of string, that are stored in an Async-Local structure (Similar to "Thread Context" in Log4j). It is the async version of NDC Layout Renderer

Platforms Supported: All

Introduced in NLog 4.4.1

It enables one to assign a scope-name to the active scope (Ex. a request method-name). Then all logger-events created within the scoped logical context, can automatically capture the scope-name without needing to specify it with each LogEvent. The specified scope states will automatically flow together with async Tasks.

See also NLog Context.

Configuration Syntax

${ndlc: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

using (NLog.NestedDiagnosticsLogicalContext.Push("Outer Scope"))
{
   Logger.Info("Hello Outer");
   await InnerOperationAsync();
}

static async Task InnerOperationAsync()
{
    using (NLog.NestedDiagnosticsLogicalContext.Push("Inner Scope"))
    {
        Logger.Info("Hello Inner");
        await Task.Yield();
    }
}
Clone this wiki locally