LevelMapping.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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.Core;
  22. namespace log4net.Util
  23. {
  24. /// <summary>
  25. /// Manages a mapping from levels to <see cref="LevelMappingEntry"/>
  26. /// </summary>
  27. /// <remarks>
  28. /// <para>
  29. /// Manages an ordered mapping from <see cref="Level"/> instances
  30. /// to <see cref="LevelMappingEntry"/> subclasses.
  31. /// </para>
  32. /// </remarks>
  33. /// <author>Nicko Cadell</author>
  34. public sealed class LevelMapping : IOptionHandler
  35. {
  36. #region Public Instance Constructors
  37. /// <summary>
  38. /// Default constructor
  39. /// </summary>
  40. /// <remarks>
  41. /// <para>
  42. /// Initialise a new instance of <see cref="LevelMapping"/>.
  43. /// </para>
  44. /// </remarks>
  45. public LevelMapping()
  46. {
  47. }
  48. #endregion // Public Instance Constructors
  49. #region Public Instance Methods
  50. /// <summary>
  51. /// Add a <see cref="LevelMappingEntry"/> to this mapping
  52. /// </summary>
  53. /// <param name="entry">the entry to add</param>
  54. /// <remarks>
  55. /// <para>
  56. /// If a <see cref="LevelMappingEntry"/> has previously been added
  57. /// for the same <see cref="Level"/> then that entry will be
  58. /// overwritten.
  59. /// </para>
  60. /// </remarks>
  61. public void Add(LevelMappingEntry entry)
  62. {
  63. if (m_entriesMap.ContainsKey(entry.Level))
  64. {
  65. m_entriesMap.Remove(entry.Level);
  66. }
  67. m_entriesMap.Add(entry.Level, entry);
  68. }
  69. /// <summary>
  70. /// Lookup the mapping for the specified level
  71. /// </summary>
  72. /// <param name="level">the level to lookup</param>
  73. /// <returns>the <see cref="LevelMappingEntry"/> for the level or <c>null</c> if no mapping found</returns>
  74. /// <remarks>
  75. /// <para>
  76. /// Lookup the value for the specified level. Finds the nearest
  77. /// mapping value for the level that is equal to or less than the
  78. /// <paramref name="level"/> specified.
  79. /// </para>
  80. /// <para>
  81. /// If no mapping could be found then <c>null</c> is returned.
  82. /// </para>
  83. /// </remarks>
  84. public LevelMappingEntry Lookup(Level level)
  85. {
  86. if (m_entries != null)
  87. {
  88. foreach(LevelMappingEntry entry in m_entries)
  89. {
  90. if (level >= entry.Level)
  91. {
  92. return entry;
  93. }
  94. }
  95. }
  96. return null;
  97. }
  98. #endregion // Public Instance Methods
  99. #region IOptionHandler Members
  100. /// <summary>
  101. /// Initialize options
  102. /// </summary>
  103. /// <remarks>
  104. /// <para>
  105. /// Caches the sorted list of <see cref="LevelMappingEntry"/> in an array
  106. /// </para>
  107. /// </remarks>
  108. public void ActivateOptions()
  109. {
  110. Level[] sortKeys = new Level[m_entriesMap.Count];
  111. LevelMappingEntry[] sortValues = new LevelMappingEntry[m_entriesMap.Count];
  112. m_entriesMap.Keys.CopyTo(sortKeys, 0);
  113. m_entriesMap.Values.CopyTo(sortValues, 0);
  114. // Sort in level order
  115. Array.Sort(sortKeys, sortValues, 0, sortKeys.Length, null);
  116. // Reverse list so that highest level is first
  117. Array.Reverse(sortValues, 0, sortValues.Length);
  118. foreach(LevelMappingEntry entry in sortValues)
  119. {
  120. entry.ActivateOptions();
  121. }
  122. m_entries = sortValues;
  123. }
  124. #endregion // IOptionHandler Members
  125. #region Private Instance Fields
  126. private Hashtable m_entriesMap = new Hashtable();
  127. private LevelMappingEntry[] m_entries = null;
  128. #endregion // Private Instance Fields
  129. }
  130. }