Class NDC
- Namespace
- log4net
- Assembly
- log4net.dll
Implementation of Nested Diagnostic Contexts.
public sealed class NDC
- Inheritance
-
NDC
- Inherited Members
Examples
How to push a message into the context
<pre><code class="lang-csharp">using(NDC.Push("my context message"))
{ ... all log calls will have 'my context message' included ...
} // at the end of the using block the message is automatically removed
Remarks
note
The NDC is deprecated and has been replaced by the Stacks.
The current NDC implementation forwards to the ThreadContext.Stacks["NDC"].
A Nested Diagnostic Context, or NDC in short, is an instrument to distinguish interleaved log output from different sources. Log output is typically interleaved when a server handles multiple clients near-simultaneously.
Interleaved log output can still be meaningful if each log entry from different contexts had a distinctive stamp. This is where NDCs come into play.
Note that NDCs are managed on a per thread basis. The NDC class is made up of static methods that operate on the context of the calling thread.
Properties
Depth
Gets the current context depth.
public static int Depth { get; }
Property Value
- int
The current context depth.
Remarks
note
The NDC is deprecated and has been replaced by the Stacks.
The current NDC implementation forwards to the ThreadContext.Stacks["NDC"].
The number of context values pushed onto the context stack.
Used to record the current depth of the context. This can then be restored using the SetMaxDepth(int) method.
- See Also
Methods
Clear()
Clears all the contextual information held on the current thread.
public static void Clear()
Remarks
note
The NDC is deprecated and has been replaced by the Stacks.
The current NDC implementation forwards to the ThreadContext.Stacks["NDC"].
Clears the stack of NDC data held on the current thread.
CloneStack()
Creates a clone of the stack of context information.
public static Stack CloneStack()
Returns
- Stack
A clone of the context info for this thread.
Remarks
note
The NDC is deprecated and has been replaced by the Stacks.
The current NDC implementation forwards to the ThreadContext.Stacks["NDC"].
The results of this method can be passed to the Inherit(Stack) method to allow child threads to inherit the context of their parent thread.
Inherit(Stack)
Inherits the contextual information from another thread.
public static void Inherit(Stack stack)
Parameters
stackStackThe context stack to inherit.
Remarks
note
The NDC is deprecated and has been replaced by the Stacks.
The current NDC implementation forwards to the ThreadContext.Stacks["NDC"].
This thread will use the context information from the stack supplied. This can be used to initialize child threads with the same contextual information as their parent threads. These contexts will NOT be shared. Any further contexts that are pushed onto the stack will not be visible to the other. Call CloneStack() to obtain a stack to pass to this method.
Pop()
Removes the top context from the stack.
public static string Pop()
Returns
- string
The message in the context that was removed from the top of the stack.
Remarks
note
The NDC is deprecated and has been replaced by the Stacks.
The current NDC implementation forwards to the ThreadContext.Stacks["NDC"].
Remove the top context from the stack, and return
it to the caller. If the stack is empty then an
empty string (not null) is returned.
Push(string)
Pushes a new context message.
public static IDisposable Push(string message)
Parameters
messagestringThe new context message.
Returns
- IDisposable
An IDisposable that can be used to clean up the context stack.
Examples
Simple example of using the Push method with the using keyword.
<pre><code class="lang-csharp">using(log4net.NDC.Push("NDC_Message"))
{ log.Warn("This should have an NDC message"); }
Remarks
note
The NDC is deprecated and has been replaced by the Stacks.
The current NDC implementation forwards to the ThreadContext.Stacks["NDC"].
Pushes a new context onto the context stack. An IDisposable
is returned that can be used to clean up the context stack. This
can be easily combined with the using keyword to scope the
context.
Remove()
Removes the context information for this thread. It is not required to call this method.
public static void Remove()
Remarks
note
The NDC is deprecated and has been replaced by the Stacks.
The current NDC implementation forwards to the ThreadContext.Stacks["NDC"].
This method is not implemented.
SetMaxDepth(int)
Forces the stack depth to be at most maxDepth.
public static void SetMaxDepth(int maxDepth)
Parameters
maxDepthintThe maximum depth of the stack
Remarks
note
The NDC is deprecated and has been replaced by the Stacks.
The current NDC implementation forwards to the ThreadContext.Stacks["NDC"].
Forces the stack depth to be at most maxDepth.
This may truncate the head of the stack. This only affects the
stack in the current thread. Also it does not prevent it from
growing, it only sets the maximum depth at the time of the
call. This can be used to return to a known context depth.