LogicalThreadContext Class | Apache log4net™ SDK Documentation

The log4net Logical Thread Context.
Inheritance Hierarchy

System
Object
log4net
LogicalThreadContext

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

C#
VB
C++
F#
Copy

    public sealed class LogicalThreadContext

    Public NotInheritable Class LogicalThreadContext

    public ref class LogicalThreadContext sealed

    [<SealedAttribute>]
    type LogicalThreadContext =  class end

The LogicalThreadContext type exposes the following members.

Properties

| Name | Description
--- | --- | ---
| Properties | The thread properties map
| Stacks | The thread stacks

Top
Methods

| Name | Description
--- | --- | ---
| Equals | Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
| GetHashCode | Serves as a hash function for a particular type. (Inherited from Object.)
| GetType | Gets the Type of the current instance. (Inherited from Object.)
| ToString | Returns a String that represents the current Object. (Inherited from Object.)

Top
Remarks

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

For .NET Standard 1.3 this class uses System.Threading.AsyncLocal rather than CallContext.

The Logical 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 Logical Thread Context provides a diagnostic context for the current call context. 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 Logical Thread Context is managed on a per CallContext basis.

The CallContext requires a link time SecurityPermission for the Infrastructure. If the calling code does not have this permission then this context will be disabled. It will not store any property values set on it.

Examples

Example of using the thread context properties to store a username.
C#
Copy

    LogicalThreadContext.Properties["user"] = userName;
    log.Info("This log message has a LogicalThreadContext Property called 'user'");

Examples

Example of how to push a message into the context stack
C#
Copy

    using(LogicalThreadContext.Stacks["LDC"].Push("my context message"))
    {
        log.Info("This log message has a LogicalThreadContext 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

#### Reference

log4net Namespace