1 #region Copyright & License 2 // 3 // Copyright 2001-2005 The Apache Software Foundation 4 // 5 // Licensed under the Apache License, Version 2.0 (the "License"); 6 // you may not use this file except in compliance with the License. 7 // You may obtain a copy of the License at 8 // 9 // http://www.apache.org/licenses/LICENSE-2.0 10 // 11 // Unless required by applicable law or agreed to in writing, software 12 // distributed under the License is distributed on an "AS IS" BASIS, 13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 // See the License for the specific language governing permissions and 15 // limitations under the License. 16 // 17 #endregion 18 19 // .NET Compact Framework 1.0 has no support for System.Runtime.Remoting.Messaging.CallContext 20 #if !NETCF 21 22 using System; 23 using System.Collections; 24 25 using log4net.Util; 26 27 namespace log4net 28 { 29 /// <summary> 30 /// The log4net Logical Thread Context. 31 /// </summary> 32 /// <remarks> 33 /// <para> 34 /// The <c>LogicalThreadContext</c> provides a location for <see cref="System.Runtime.Remoting.Messaging.CallContext"/> specific debugging 35 /// information to be stored. 36 /// The <c>LogicalThreadContext</c> properties override any <see cref="ThreadContext"/> or <see cref="GlobalContext"/> 37 /// properties with the same name. 38 /// </para> 39 /// <para> 40 /// The Logical Thread Context has a properties map and a stack. 41 /// The properties and stack can 42 /// be included in the output of log messages. The <see cref="log4net.Layout.PatternLayout"/> 43 /// supports selecting and outputting these properties. 44 /// </para> 45 /// <para> 46 /// The Logical Thread Context provides a diagnostic context for the current call context. 47 /// This is an instrument for distinguishing interleaved log 48 /// output from different sources. Log output is typically interleaved 49 /// when a server handles multiple clients near-simultaneously. 50 /// </para> 51 /// <para> 52 /// The Logical Thread Context is managed on a per <see cref="System.Runtime.Remoting.Messaging.CallContext"/> basis. 53 /// </para> 54 /// </remarks> 55 /// <example>Example of using the thread context properties to store a username. 56 /// <code lang="C#"> 57 /// LogicalThreadContext.Properties["user"] = userName; 58 /// log.Info("This log message has a LogicalThreadContext Property called 'user'"); 59 /// </code> 60 /// </example> 61 /// <example>Example of how to push a message into the context stack 62 /// <code lang="C#"> 63 /// using(LogicalThreadContext.Stacks["LDC"].Push("my context message")) 64 /// { 65 /// log.Info("This log message has a LogicalThreadContext Stack message that includes 'my context message'"); 66 /// 67 /// } // at the end of the using block the message is automatically popped 68 /// </code> 69 /// </example> 70 /// <threadsafety static="true" instance="true" /> 71 /// <author>Nicko Cadell</author> 72 public sealed class LogicalThreadContext 73 { 74 #region Private Instance Constructors 75 76 /// <summary> 77 /// Private Constructor. 78 /// </summary> 79 /// <remarks> 80 /// <para> 81 /// Uses a private access modifier to prevent instantiation of this class. 82 /// </para> 83 /// </remarks> LogicalThreadContext()84 private LogicalThreadContext() 85 { 86 } 87 88 #endregion Private Instance Constructors 89 90 #region Public Static Properties 91 92 /// <summary> 93 /// The thread properties map 94 /// </summary> 95 /// <value> 96 /// The thread properties map 97 /// </value> 98 /// <remarks> 99 /// <para> 100 /// The <c>LogicalThreadContext</c> properties override any <see cref="ThreadContext"/> 101 /// or <see cref="GlobalContext"/> properties with the same name. 102 /// </para> 103 /// </remarks> 104 public static LogicalThreadContextProperties Properties 105 { 106 get { return s_properties; } 107 } 108 109 /// <summary> 110 /// The thread stacks 111 /// </summary> 112 /// <value> 113 /// stack map 114 /// </value> 115 /// <remarks> 116 /// <para> 117 /// The logical thread stacks. 118 /// </para> 119 /// </remarks> 120 public static ThreadContextStacks Stacks 121 { 122 get { return s_stacks; } 123 } 124 125 #endregion Public Static Properties 126 127 #region Private Static Fields 128 129 /// <summary> 130 /// The thread context properties instance 131 /// </summary> 132 private readonly static LogicalThreadContextProperties s_properties = new LogicalThreadContextProperties(); 133 134 /// <summary> 135 /// The thread context stacks instance 136 /// </summary> 137 private readonly static ThreadContextStacks s_stacks = new ThreadContextStacks(s_properties); 138 139 #endregion Private Static Fields 140 } 141 } 142 143 #endif