ThreadContextStacks.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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.Util
  22. {
  23. /// <summary>
  24. /// Implementation of Stacks collection for the <see cref="log4net.ThreadContext"/>
  25. /// </summary>
  26. /// <remarks>
  27. /// <para>
  28. /// Implementation of Stacks collection for the <see cref="log4net.ThreadContext"/>
  29. /// </para>
  30. /// </remarks>
  31. /// <author>Nicko Cadell</author>
  32. public sealed class ThreadContextStacks
  33. {
  34. private readonly ContextPropertiesBase m_properties;
  35. #region Public Instance Constructors
  36. /// <summary>
  37. /// Internal constructor
  38. /// </summary>
  39. /// <remarks>
  40. /// <para>
  41. /// Initializes a new instance of the <see cref="ThreadContextStacks" /> class.
  42. /// </para>
  43. /// </remarks>
  44. internal ThreadContextStacks(ContextPropertiesBase properties)
  45. {
  46. m_properties = properties;
  47. }
  48. #endregion Public Instance Constructors
  49. #region Public Instance Properties
  50. /// <summary>
  51. /// Gets the named thread context stack
  52. /// </summary>
  53. /// <value>
  54. /// The named stack
  55. /// </value>
  56. /// <remarks>
  57. /// <para>
  58. /// Gets the named thread context stack
  59. /// </para>
  60. /// </remarks>
  61. public ThreadContextStack this[string key]
  62. {
  63. get
  64. {
  65. ThreadContextStack stack = null;
  66. object propertyValue = m_properties[key];
  67. if (propertyValue == null)
  68. {
  69. // Stack does not exist, create
  70. stack = new ThreadContextStack();
  71. m_properties[key] = stack;
  72. }
  73. else
  74. {
  75. // Look for existing stack
  76. stack = propertyValue as ThreadContextStack;
  77. if (stack == null)
  78. {
  79. // Property is not set to a stack!
  80. string propertyValueString = SystemInfo.NullText;
  81. try
  82. {
  83. propertyValueString = propertyValue.ToString();
  84. }
  85. catch
  86. {
  87. }
  88. 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+"]");
  89. stack = new ThreadContextStack();
  90. }
  91. }
  92. return stack;
  93. }
  94. }
  95. #endregion Public Instance Properties
  96. #region Private Static Fields
  97. /// <summary>
  98. /// The fully qualified type of the ThreadContextStacks class.
  99. /// </summary>
  100. /// <remarks>
  101. /// Used by the internal logger to record the Type of the
  102. /// log message.
  103. /// </remarks>
  104. private readonly static Type declaringType = typeof(ThreadContextStacks);
  105. #endregion Private Static Fields
  106. }
  107. }