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

ScopeNested Layout Renderer

Rolf Kristensen edited this pageOct 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 usesRemoting.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 alsoNLog 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");awaitInnerOperationAsync();}staticasyncTaskInnerOperationAsync(){using(NLog.ScopeContext.PushNestedState("Inner Scope")){Logger.Info("Hello Inner");awaitTask.Yield();}}

The NLogLogger can also be used to update ScopeContext:

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

.NET Core logging

When using NLog.Extensions.Logging or NLog.Web.AspNetCore, you can also useBeginScope andmore 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}

-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