MDC.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. namespace log4net
  22. {
  23. /// <summary>
  24. /// Implementation of Mapped Diagnostic Contexts.
  25. /// </summary>
  26. /// <remarks>
  27. /// <note>
  28. /// <para>
  29. /// The MDC is deprecated and has been replaced by the <see cref="ThreadContext.Properties"/>.
  30. /// The current MDC implementation forwards to the <c>ThreadContext.Properties</c>.
  31. /// </para>
  32. /// </note>
  33. /// <para>
  34. /// The MDC class is similar to the <see cref="NDC"/> class except that it is
  35. /// based on a map instead of a stack. It provides <i>mapped
  36. /// diagnostic contexts</i>. A <i>Mapped Diagnostic Context</i>, or
  37. /// MDC in short, is an instrument for distinguishing interleaved log
  38. /// output from different sources. Log output is typically interleaved
  39. /// when a server handles multiple clients near-simultaneously.
  40. /// </para>
  41. /// <para>
  42. /// The MDC is managed on a per thread basis.
  43. /// </para>
  44. /// </remarks>
  45. /// <threadsafety static="true" instance="true" />
  46. /// <author>Nicko Cadell</author>
  47. /// <author>Gert Driesen</author>
  48. /*[Obsolete("MDC has been replaced by ThreadContext.Properties")]*/
  49. public sealed class MDC
  50. {
  51. #region Private Instance Constructors
  52. /// <summary>
  53. /// Initializes a new instance of the <see cref="MDC" /> class.
  54. /// </summary>
  55. /// <remarks>
  56. /// Uses a private access modifier to prevent instantiation of this class.
  57. /// </remarks>
  58. private MDC()
  59. {
  60. }
  61. #endregion Private Instance Constructors
  62. #region Public Static Methods
  63. /// <summary>
  64. /// Gets the context value identified by the <paramref name="key" /> parameter.
  65. /// </summary>
  66. /// <param name="key">The key to lookup in the MDC.</param>
  67. /// <returns>The string value held for the key, or a <c>null</c> reference if no corresponding value is found.</returns>
  68. /// <remarks>
  69. /// <note>
  70. /// <para>
  71. /// The MDC is deprecated and has been replaced by the <see cref="ThreadContext.Properties"/>.
  72. /// The current MDC implementation forwards to the <c>ThreadContext.Properties</c>.
  73. /// </para>
  74. /// </note>
  75. /// <para>
  76. /// If the <paramref name="key" /> parameter does not look up to a
  77. /// previously defined context then <c>null</c> will be returned.
  78. /// </para>
  79. /// </remarks>
  80. /*[Obsolete("MDC has been replaced by ThreadContext.Properties")]*/
  81. public static string Get(string key)
  82. {
  83. object obj = ThreadContext.Properties[key];
  84. if (obj == null)
  85. {
  86. return null;
  87. }
  88. return obj.ToString();
  89. }
  90. /// <summary>
  91. /// Add an entry to the MDC
  92. /// </summary>
  93. /// <param name="key">The key to store the value under.</param>
  94. /// <param name="value">The value to store.</param>
  95. /// <remarks>
  96. /// <note>
  97. /// <para>
  98. /// The MDC is deprecated and has been replaced by the <see cref="ThreadContext.Properties"/>.
  99. /// The current MDC implementation forwards to the <c>ThreadContext.Properties</c>.
  100. /// </para>
  101. /// </note>
  102. /// <para>
  103. /// Puts a context value (the <paramref name="value" /> parameter) as identified
  104. /// with the <paramref name="key" /> parameter into the current thread's
  105. /// context map.
  106. /// </para>
  107. /// <para>
  108. /// If a value is already defined for the <paramref name="key" />
  109. /// specified then the value will be replaced. If the <paramref name="value" />
  110. /// is specified as <c>null</c> then the key value mapping will be removed.
  111. /// </para>
  112. /// </remarks>
  113. /*[Obsolete("MDC has been replaced by ThreadContext.Properties")]*/
  114. public static void Set(string key, string value)
  115. {
  116. ThreadContext.Properties[key] = value;
  117. }
  118. /// <summary>
  119. /// Removes the key value mapping for the key specified.
  120. /// </summary>
  121. /// <param name="key">The key to remove.</param>
  122. /// <remarks>
  123. /// <note>
  124. /// <para>
  125. /// The MDC is deprecated and has been replaced by the <see cref="ThreadContext.Properties"/>.
  126. /// The current MDC implementation forwards to the <c>ThreadContext.Properties</c>.
  127. /// </para>
  128. /// </note>
  129. /// <para>
  130. /// Remove the specified entry from this thread's MDC
  131. /// </para>
  132. /// </remarks>
  133. /*[Obsolete("MDC has been replaced by ThreadContext.Properties")]*/
  134. public static void Remove(string key)
  135. {
  136. ThreadContext.Properties.Remove(key);
  137. }
  138. /// <summary>
  139. /// Clear all entries in the MDC
  140. /// </summary>
  141. /// <remarks>
  142. /// <note>
  143. /// <para>
  144. /// The MDC is deprecated and has been replaced by the <see cref="ThreadContext.Properties"/>.
  145. /// The current MDC implementation forwards to the <c>ThreadContext.Properties</c>.
  146. /// </para>
  147. /// </note>
  148. /// <para>
  149. /// Remove all the entries from this thread's MDC
  150. /// </para>
  151. /// </remarks>
  152. /*[Obsolete("MDC has been replaced by ThreadContext.Properties")]*/
  153. public static void Clear()
  154. {
  155. ThreadContext.Properties.Clear();
  156. }
  157. #endregion Public Static Methods
  158. }
  159. }