NDC ClassApache log4net™ SDK Documentation
Implementation of Nested Diagnostic Contexts.
Inheritance Hierarchy

SystemObject
  log4netNDC

Namespace: log4net
Assembly: log4net (in log4net.dll) Version: 2.0.6.0-.NET 4.0
Syntax

public sealed class NDC

The NDC type exposes the following members.

Properties

  NameDescription
Public propertyStatic memberDepth
Gets the current context depth.
Top
Methods

  NameDescription
Public methodStatic memberClear
Clears all the contextual information held on the current thread.
Public methodStatic memberCloneStack
Creates a clone of the stack of context information.
Public methodEquals
Determines whether the specified Object is equal to the current Object.
(Inherited from Object.)
Public methodGetHashCode
Serves as a hash function for a particular type.
(Inherited from Object.)
Public methodGetType
Gets the Type of the current instance.
(Inherited from Object.)
Public methodStatic memberInherit
Inherits the contextual information from another thread.
Public methodStatic memberPop
Removes the top context from the stack.
Public methodStatic memberCode examplePush
Pushes a new context message.
Public methodStatic memberCode examplePushFormat
Pushes a new context message.
Public methodStatic memberRemove
Removes the context information for this thread. It is not required to call this method.
Public methodStatic memberSetMaxDepth
Forces the stack depth to be at most maxDepth.
Public methodToString
Returns a String that represents the current Object.
(Inherited from Object.)
Top
Remarks

Note 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.

Examples

How to push a message into the context
C#
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
Thread Safety

Static members of this type are safe for multi-threaded operations. Instance members of this type are safe for multi-threaded operations.
See Also

Reference