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

Appearance settings

ScopeNested Layout Renderer

Rolf Kristensen edited this page Oct 26, 2025 · 12 revisions

ScopeContext Nested States are stored in the thread execution context. Similar to "Nested Diagnostic Context" (NDC) in Log4j.

Platforms Supported: All (AsyncLocal is used for NetStandard and Net46, but older platforms uses Remoting.Messaging.CallContext)

Introduced with NLog 5.0 that merges ${MDC} + ${MDLC} + ${NDC} + ${NDLC} into an unified ScopeContext.

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 and ${scopeproperty} and ${scopetiming} and ${scopeindent}

Configuration Syntax

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

Parameters

  • 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)
  • format - Format string for conversion into string. Possible to use @ for Json-Array.
  • culture - Format provider for conversion into string.

Examples

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

static async Task InnerOperationAsync()
{
    using (NLog.ScopeContext.PushNestedState("Inner Scope"))
    {
        Logger.Info("Hello Inner");
        await Task.Yield();
    }
}

The NLog Logger can also be used to update ScopeContext:

var logger = NLog.LogManager.GetCurrentClassLogger();
using (logger.PushScopeNested("Outer Scope"))
{
}

.NET Core logging

When using NLog.Extensions.Logging or NLog.Web.AspNetCore, you can also use BeginScope and more advanced options:

using (_logger.BeginScope("Outer Scope"))
{
   _logger.LogDebug("Start process {ProccessName}, "Main");
}

Indenting Log Messages

Indent log-messages based on how many nested levels in ScopeContext using ${scopeindent}

${scopeindent}${message}

Clone this wiki locally

Morty Proxy This is a proxified and sanitized view of the page, visit original site.