LevelMap.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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 System.Collections.Specialized;
  22. using log4net.Util;
  23. namespace log4net.Core
  24. {
  25. /// <summary>
  26. /// Mapping between string name and Level object
  27. /// </summary>
  28. /// <remarks>
  29. /// <para>
  30. /// Mapping between string name and <see cref="Level"/> object.
  31. /// This mapping is held separately for each <see cref="log4net.Repository.ILoggerRepository"/>.
  32. /// The level name is case insensitive.
  33. /// </para>
  34. /// </remarks>
  35. /// <author>Nicko Cadell</author>
  36. public sealed class LevelMap
  37. {
  38. #region Member Variables
  39. /// <summary>
  40. /// Mapping from level name to Level object. The
  41. /// level name is case insensitive
  42. /// </summary>
  43. private Hashtable m_mapName2Level = SystemInfo.CreateCaseInsensitiveHashtable();
  44. #endregion
  45. /// <summary>
  46. /// Construct the level map
  47. /// </summary>
  48. /// <remarks>
  49. /// <para>
  50. /// Construct the level map.
  51. /// </para>
  52. /// </remarks>
  53. public LevelMap()
  54. {
  55. }
  56. /// <summary>
  57. /// Clear the internal maps of all levels
  58. /// </summary>
  59. /// <remarks>
  60. /// <para>
  61. /// Clear the internal maps of all levels
  62. /// </para>
  63. /// </remarks>
  64. public void Clear()
  65. {
  66. // Clear all current levels
  67. m_mapName2Level.Clear();
  68. }
  69. /// <summary>
  70. /// Lookup a <see cref="Level"/> by name
  71. /// </summary>
  72. /// <param name="name">The name of the Level to lookup</param>
  73. /// <returns>a Level from the map with the name specified</returns>
  74. /// <remarks>
  75. /// <para>
  76. /// Returns the <see cref="Level"/> from the
  77. /// map with the name specified. If the no level is
  78. /// found then <c>null</c> is returned.
  79. /// </para>
  80. /// </remarks>
  81. public Level this[string name]
  82. {
  83. get
  84. {
  85. if (name == null)
  86. {
  87. throw new ArgumentNullException("name");
  88. }
  89. lock(this)
  90. {
  91. return (Level)m_mapName2Level[name];
  92. }
  93. }
  94. }
  95. /// <summary>
  96. /// Create a new Level and add it to the map
  97. /// </summary>
  98. /// <param name="name">the string to display for the Level</param>
  99. /// <param name="value">the level value to give to the Level</param>
  100. /// <remarks>
  101. /// <para>
  102. /// Create a new Level and add it to the map
  103. /// </para>
  104. /// </remarks>
  105. /// <seealso cref="M:Add(string,int,string)"/>
  106. public void Add(string name, int value)
  107. {
  108. Add(name, value, null);
  109. }
  110. /// <summary>
  111. /// Create a new Level and add it to the map
  112. /// </summary>
  113. /// <param name="name">the string to display for the Level</param>
  114. /// <param name="value">the level value to give to the Level</param>
  115. /// <param name="displayName">the display name to give to the Level</param>
  116. /// <remarks>
  117. /// <para>
  118. /// Create a new Level and add it to the map
  119. /// </para>
  120. /// </remarks>
  121. public void Add(string name, int value, string displayName)
  122. {
  123. if (name == null)
  124. {
  125. throw new ArgumentNullException("name");
  126. }
  127. if (name.Length == 0)
  128. {
  129. throw log4net.Util.SystemInfo.CreateArgumentOutOfRangeException("name", name, "Parameter: name, Value: ["+name+"] out of range. Level name must not be empty");
  130. }
  131. if (displayName == null || displayName.Length == 0)
  132. {
  133. displayName = name;
  134. }
  135. Add(new Level(value, name, displayName));
  136. }
  137. /// <summary>
  138. /// Add a Level to the map
  139. /// </summary>
  140. /// <param name="level">the Level to add</param>
  141. /// <remarks>
  142. /// <para>
  143. /// Add a Level to the map
  144. /// </para>
  145. /// </remarks>
  146. public void Add(Level level)
  147. {
  148. if (level == null)
  149. {
  150. throw new ArgumentNullException("level");
  151. }
  152. lock(this)
  153. {
  154. m_mapName2Level[level.Name] = level;
  155. }
  156. }
  157. /// <summary>
  158. /// Return all possible levels as a list of Level objects.
  159. /// </summary>
  160. /// <returns>all possible levels as a list of Level objects</returns>
  161. /// <remarks>
  162. /// <para>
  163. /// Return all possible levels as a list of Level objects.
  164. /// </para>
  165. /// </remarks>
  166. public LevelCollection AllLevels
  167. {
  168. get
  169. {
  170. lock(this)
  171. {
  172. return new LevelCollection(m_mapName2Level.Values);
  173. }
  174. }
  175. }
  176. /// <summary>
  177. /// Lookup a named level from the map
  178. /// </summary>
  179. /// <param name="defaultLevel">the name of the level to lookup is taken from this level.
  180. /// If the level is not set on the map then this level is added</param>
  181. /// <returns>the level in the map with the name specified</returns>
  182. /// <remarks>
  183. /// <para>
  184. /// Lookup a named level from the map. The name of the level to lookup is taken
  185. /// from the <see cref="Level.Name"/> property of the <paramref name="defaultLevel"/>
  186. /// argument.
  187. /// </para>
  188. /// <para>
  189. /// If no level with the specified name is found then the
  190. /// <paramref name="defaultLevel"/> argument is added to the level map
  191. /// and returned.
  192. /// </para>
  193. /// </remarks>
  194. public Level LookupWithDefault(Level defaultLevel)
  195. {
  196. if (defaultLevel == null)
  197. {
  198. throw new ArgumentNullException("defaultLevel");
  199. }
  200. lock(this)
  201. {
  202. Level level = (Level)m_mapName2Level[defaultLevel.Name];
  203. if (level == null)
  204. {
  205. m_mapName2Level[defaultLevel.Name] = defaultLevel;
  206. return defaultLevel;
  207. }
  208. return level;
  209. }
  210. }
  211. }
  212. }