LogicalThreadContextStacks.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. #if !NETCF
  20. using System;
  21. using System.Collections;
  22. namespace log4net.Util
  23. {
  24. /// <summary>
  25. /// Implementation of Stacks collection for the <see cref="log4net.LogicalThreadContext"/>
  26. /// </summary>
  27. /// <remarks>
  28. /// <para>
  29. /// Implementation of Stacks collection for the <see cref="log4net.LogicalThreadContext"/>
  30. /// </para>
  31. /// </remarks>
  32. /// <author>Nicko Cadell</author>
  33. public sealed class LogicalThreadContextStacks
  34. {
  35. private readonly LogicalThreadContextProperties m_properties;
  36. #region Public Instance Constructors
  37. /// <summary>
  38. /// Internal constructor
  39. /// </summary>
  40. /// <remarks>
  41. /// <para>
  42. /// Initializes a new instance of the <see cref="ThreadContextStacks" /> class.
  43. /// </para>
  44. /// </remarks>
  45. internal LogicalThreadContextStacks(LogicalThreadContextProperties properties)
  46. {
  47. m_properties = properties;
  48. }
  49. #endregion Public Instance Constructors
  50. #region Public Instance Properties
  51. /// <summary>
  52. /// Gets the named thread context stack
  53. /// </summary>
  54. /// <value>
  55. /// The named stack
  56. /// </value>
  57. /// <remarks>
  58. /// <para>
  59. /// Gets the named thread context stack
  60. /// </para>
  61. /// </remarks>
  62. public LogicalThreadContextStack this[string key]
  63. {
  64. get
  65. {
  66. LogicalThreadContextStack stack = null;
  67. object propertyValue = m_properties[key];
  68. if (propertyValue == null)
  69. {
  70. // Stack does not exist, create
  71. #if NET_2_0 || MONO_2_0
  72. stack = new LogicalThreadContextStack(key, registerNew);
  73. #else
  74. stack = new LogicalThreadContextStack(key, new TwoArgAction(registerNew));
  75. #endif
  76. m_properties[key] = stack;
  77. }
  78. else
  79. {
  80. // Look for existing stack
  81. stack = propertyValue as LogicalThreadContextStack;
  82. if (stack == null)
  83. {
  84. // Property is not set to a stack!
  85. string propertyValueString = SystemInfo.NullText;
  86. try
  87. {
  88. propertyValueString = propertyValue.ToString();
  89. }
  90. catch
  91. {
  92. }
  93. LogLog.Error(declaringType, "ThreadContextStacks: Request for stack named [" + key + "] failed because a property with the same name exists which is a [" + propertyValue.GetType().Name + "] with value [" + propertyValueString + "]");
  94. #if NET_2_0 || MONO_2_0
  95. stack = new LogicalThreadContextStack(key, registerNew);
  96. #else
  97. stack = new LogicalThreadContextStack(key, new TwoArgAction(registerNew));
  98. #endif
  99. }
  100. }
  101. return stack;
  102. }
  103. }
  104. #endregion Public Instance Properties
  105. #region Private Instance Fields
  106. private void registerNew(string stackName, LogicalThreadContextStack stack)
  107. {
  108. m_properties[stackName] = stack;
  109. }
  110. #endregion Private Instance Fields
  111. #region Private Static Fields
  112. /// <summary>
  113. /// The fully qualified type of the ThreadContextStacks class.
  114. /// </summary>
  115. /// <remarks>
  116. /// Used by the internal logger to record the Type of the
  117. /// log message.
  118. /// </remarks>
  119. private readonly static Type declaringType = typeof(LogicalThreadContextStacks);
  120. #endregion Private Static Fields
  121. }
  122. }
  123. #endif