ThreadContext ClassApache log4net™ SDK Documentation
The log4net Thread Context.
Inheritance Hierarchy


Namespace: log4net
Assembly: log4net (in log4net.dll) Version: 4.0

public sealed class ThreadContext

The ThreadContext type exposes the following members.


Public propertyStatic memberProperties
The thread properties map
Public propertyStatic memberStacks
The thread stacks

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 methodToString
Returns a String that represents the current Object.
(Inherited from Object.)

The ThreadContext provides a location for thread specific debugging information to be stored. The ThreadContext properties override any GlobalContext properties with the same name.

The thread context has a properties map and a stack. The properties and stack can be included in the output of log messages. The PatternLayout supports selecting and outputting these properties.

The Thread Context provides a diagnostic context for the current thread. This is an instrument for distinguishing interleaved log output from different sources. Log output is typically interleaved when a server handles multiple clients near-simultaneously.

The Thread Context is managed on a per thread basis.


Example of using the thread context properties to store a username.
ThreadContext.Properties["user"] = userName;
log.Info("This log message has a ThreadContext Property called 'user'");

Example of how to push a message into the context stack
using(ThreadContext.Stacks["NDC"].Push("my context message"))
    log.Info("This log message has a ThreadContext Stack message that includes 'my context message'");

} // at the end of the using block the message is automatically popped
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