ThreadContext.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. using System;
  20. using System.Collections;
  21. using log4net.Util;
  22. namespace log4net
  23. {
  24. /// <summary>
  25. /// The log4net Thread Context.
  26. /// </summary>
  27. /// <remarks>
  28. /// <para>
  29. /// The <c>ThreadContext</c> provides a location for thread specific debugging
  30. /// information to be stored.
  31. /// The <c>ThreadContext</c> properties override any <see cref="GlobalContext"/>
  32. /// properties with the same name.
  33. /// </para>
  34. /// <para>
  35. /// The thread context has a properties map and a stack.
  36. /// The properties and stack can
  37. /// be included in the output of log messages. The <see cref="log4net.Layout.PatternLayout"/>
  38. /// supports selecting and outputting these properties.
  39. /// </para>
  40. /// <para>
  41. /// The Thread Context provides a diagnostic context for the current thread.
  42. /// This is an instrument for distinguishing interleaved log
  43. /// output from different sources. Log output is typically interleaved
  44. /// when a server handles multiple clients near-simultaneously.
  45. /// </para>
  46. /// <para>
  47. /// The Thread Context is managed on a per thread basis.
  48. /// </para>
  49. /// </remarks>
  50. /// <example>Example of using the thread context properties to store a username.
  51. /// <code lang="C#">
  52. /// ThreadContext.Properties["user"] = userName;
  53. /// log.Info("This log message has a ThreadContext Property called 'user'");
  54. /// </code>
  55. /// </example>
  56. /// <example>Example of how to push a message into the context stack
  57. /// <code lang="C#">
  58. /// using(ThreadContext.Stacks["NDC"].Push("my context message"))
  59. /// {
  60. /// log.Info("This log message has a ThreadContext Stack message that includes 'my context message'");
  61. ///
  62. /// } // at the end of the using block the message is automatically popped
  63. /// </code>
  64. /// </example>
  65. /// <threadsafety static="true" instance="true" />
  66. /// <author>Nicko Cadell</author>
  67. public sealed class ThreadContext
  68. {
  69. #region Private Instance Constructors
  70. /// <summary>
  71. /// Private Constructor.
  72. /// </summary>
  73. /// <remarks>
  74. /// <para>
  75. /// Uses a private access modifier to prevent instantiation of this class.
  76. /// </para>
  77. /// </remarks>
  78. private ThreadContext()
  79. {
  80. }
  81. #endregion Private Instance Constructors
  82. #region Public Static Properties
  83. /// <summary>
  84. /// The thread properties map
  85. /// </summary>
  86. /// <value>
  87. /// The thread properties map
  88. /// </value>
  89. /// <remarks>
  90. /// <para>
  91. /// The <c>ThreadContext</c> properties override any <see cref="GlobalContext"/>
  92. /// properties with the same name.
  93. /// </para>
  94. /// </remarks>
  95. public static ThreadContextProperties Properties
  96. {
  97. get { return s_properties; }
  98. }
  99. /// <summary>
  100. /// The thread stacks
  101. /// </summary>
  102. /// <value>
  103. /// stack map
  104. /// </value>
  105. /// <remarks>
  106. /// <para>
  107. /// The thread local stacks.
  108. /// </para>
  109. /// </remarks>
  110. public static ThreadContextStacks Stacks
  111. {
  112. get { return s_stacks; }
  113. }
  114. #endregion Public Static Properties
  115. #region Private Static Fields
  116. /// <summary>
  117. /// The thread context properties instance
  118. /// </summary>
  119. private readonly static ThreadContextProperties s_properties = new ThreadContextProperties();
  120. /// <summary>
  121. /// The thread context stacks instance
  122. /// </summary>
  123. private readonly static ThreadContextStacks s_stacks = new ThreadContextStacks(s_properties);
  124. #endregion Private Static Fields
  125. }
  126. }