LogicalThreadContext.cs 5.1 KB

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