Level.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  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.Core
  22. {
  23. /// <summary>
  24. /// Defines the default set of levels recognized by the system.
  25. /// </summary>
  26. /// <remarks>
  27. /// <para>
  28. /// Each <see cref="LoggingEvent"/> has an associated <see cref="Level"/>.
  29. /// </para>
  30. /// <para>
  31. /// Levels have a numeric <see cref="Level.Value"/> that defines the relative
  32. /// ordering between levels. Two Levels with the same <see cref="Level.Value"/>
  33. /// are deemed to be equivalent.
  34. /// </para>
  35. /// <para>
  36. /// The levels that are recognized by log4net are set for each <see cref="log4net.Repository.ILoggerRepository"/>
  37. /// and each repository can have different levels defined. The levels are stored
  38. /// in the <see cref="log4net.Repository.ILoggerRepository.LevelMap"/> on the repository. Levels are
  39. /// looked up by name from the <see cref="log4net.Repository.ILoggerRepository.LevelMap"/>.
  40. /// </para>
  41. /// <para>
  42. /// When logging at level INFO the actual level used is not <see cref="Level.Info"/> but
  43. /// the value of <c>LoggerRepository.LevelMap["INFO"]</c>. The default value for this is
  44. /// <see cref="Level.Info"/>, but this can be changed by reconfiguring the level map.
  45. /// </para>
  46. /// <para>
  47. /// Each level has a <see cref="DisplayName"/> in addition to its <see cref="Name"/>. The
  48. /// <see cref="DisplayName"/> is the string that is written into the output log. By default
  49. /// the display name is the same as the level name, but this can be used to alias levels
  50. /// or to localize the log output.
  51. /// </para>
  52. /// <para>
  53. /// Some of the predefined levels recognized by the system are:
  54. /// </para>
  55. /// <list type="bullet">
  56. /// <item>
  57. /// <description><see cref="Off"/>.</description>
  58. /// </item>
  59. /// <item>
  60. /// <description><see cref="Fatal"/>.</description>
  61. /// </item>
  62. /// <item>
  63. /// <description><see cref="Error"/>.</description>
  64. /// </item>
  65. /// <item>
  66. /// <description><see cref="Warn"/>.</description>
  67. /// </item>
  68. /// <item>
  69. /// <description><see cref="Info"/>.</description>
  70. /// </item>
  71. /// <item>
  72. /// <description><see cref="Debug"/>.</description>
  73. /// </item>
  74. /// <item>
  75. /// <description><see cref="All"/>.</description>
  76. /// </item>
  77. /// </list>
  78. /// </remarks>
  79. /// <author>Nicko Cadell</author>
  80. /// <author>Gert Driesen</author>
  81. #if !NETCF
  82. [Serializable]
  83. #endif
  84. sealed public class Level : IComparable
  85. {
  86. #region Public Instance Constructors
  87. /// <summary>
  88. /// Constructor
  89. /// </summary>
  90. /// <param name="level">Integer value for this level, higher values represent more severe levels.</param>
  91. /// <param name="levelName">The string name of this level.</param>
  92. /// <param name="displayName">The display name for this level. This may be localized or otherwise different from the name</param>
  93. /// <remarks>
  94. /// <para>
  95. /// Initializes a new instance of the <see cref="Level" /> class with
  96. /// the specified level name and value.
  97. /// </para>
  98. /// </remarks>
  99. public Level(int level, string levelName, string displayName)
  100. {
  101. if (levelName == null)
  102. {
  103. throw new ArgumentNullException("levelName");
  104. }
  105. if (displayName == null)
  106. {
  107. throw new ArgumentNullException("displayName");
  108. }
  109. m_levelValue = level;
  110. #if NETSTANDARD1_3
  111. m_levelName = levelName;
  112. #else
  113. m_levelName = string.Intern(levelName);
  114. #endif
  115. m_levelDisplayName = displayName;
  116. }
  117. /// <summary>
  118. /// Constructor
  119. /// </summary>
  120. /// <param name="level">Integer value for this level, higher values represent more severe levels.</param>
  121. /// <param name="levelName">The string name of this level.</param>
  122. /// <remarks>
  123. /// <para>
  124. /// Initializes a new instance of the <see cref="Level" /> class with
  125. /// the specified level name and value.
  126. /// </para>
  127. /// </remarks>
  128. public Level(int level, string levelName) : this(level, levelName, levelName)
  129. {
  130. }
  131. #endregion Public Instance Constructors
  132. #region Public Instance Properties
  133. /// <summary>
  134. /// Gets the name of this level.
  135. /// </summary>
  136. /// <value>
  137. /// The name of this level.
  138. /// </value>
  139. /// <remarks>
  140. /// <para>
  141. /// Gets the name of this level.
  142. /// </para>
  143. /// </remarks>
  144. public string Name
  145. {
  146. get { return m_levelName; }
  147. }
  148. /// <summary>
  149. /// Gets the value of this level.
  150. /// </summary>
  151. /// <value>
  152. /// The value of this level.
  153. /// </value>
  154. /// <remarks>
  155. /// <para>
  156. /// Gets the value of this level.
  157. /// </para>
  158. /// </remarks>
  159. public int Value
  160. {
  161. get { return m_levelValue; }
  162. }
  163. /// <summary>
  164. /// Gets the display name of this level.
  165. /// </summary>
  166. /// <value>
  167. /// The display name of this level.
  168. /// </value>
  169. /// <remarks>
  170. /// <para>
  171. /// Gets the display name of this level.
  172. /// </para>
  173. /// </remarks>
  174. public string DisplayName
  175. {
  176. get { return m_levelDisplayName; }
  177. }
  178. #endregion Public Instance Properties
  179. #region Override implementation of Object
  180. /// <summary>
  181. /// Returns the <see cref="string" /> representation of the current
  182. /// <see cref="Level" />.
  183. /// </summary>
  184. /// <returns>
  185. /// A <see cref="string" /> representation of the current <see cref="Level" />.
  186. /// </returns>
  187. /// <remarks>
  188. /// <para>
  189. /// Returns the level <see cref="Name"/>.
  190. /// </para>
  191. /// </remarks>
  192. override public string ToString()
  193. {
  194. return m_levelName;
  195. }
  196. /// <summary>
  197. /// Compares levels.
  198. /// </summary>
  199. /// <param name="o">The object to compare against.</param>
  200. /// <returns><c>true</c> if the objects are equal.</returns>
  201. /// <remarks>
  202. /// <para>
  203. /// Compares the levels of <see cref="Level" /> instances, and
  204. /// defers to base class if the target object is not a <see cref="Level" />
  205. /// instance.
  206. /// </para>
  207. /// </remarks>
  208. override public bool Equals(object o)
  209. {
  210. Level otherLevel = o as Level;
  211. if (otherLevel != null)
  212. {
  213. return m_levelValue == otherLevel.m_levelValue;
  214. }
  215. else
  216. {
  217. return base.Equals(o);
  218. }
  219. }
  220. /// <summary>
  221. /// Returns a hash code
  222. /// </summary>
  223. /// <returns>A hash code for the current <see cref="Level" />.</returns>
  224. /// <remarks>
  225. /// <para>
  226. /// Returns a hash code suitable for use in hashing algorithms and data
  227. /// structures like a hash table.
  228. /// </para>
  229. /// <para>
  230. /// Returns the hash code of the level <see cref="Value"/>.
  231. /// </para>
  232. /// </remarks>
  233. override public int GetHashCode()
  234. {
  235. return m_levelValue;
  236. }
  237. #endregion Override implementation of Object
  238. #region Implementation of IComparable
  239. /// <summary>
  240. /// Compares this instance to a specified object and returns an
  241. /// indication of their relative values.
  242. /// </summary>
  243. /// <param name="r">A <see cref="Level"/> instance or <see langword="null" /> to compare with this instance.</param>
  244. /// <returns>
  245. /// A 32-bit signed integer that indicates the relative order of the
  246. /// values compared. The return value has these meanings:
  247. /// <list type="table">
  248. /// <listheader>
  249. /// <term>Value</term>
  250. /// <description>Meaning</description>
  251. /// </listheader>
  252. /// <item>
  253. /// <term>Less than zero</term>
  254. /// <description>This instance is less than <paramref name="r" />.</description>
  255. /// </item>
  256. /// <item>
  257. /// <term>Zero</term>
  258. /// <description>This instance is equal to <paramref name="r" />.</description>
  259. /// </item>
  260. /// <item>
  261. /// <term>Greater than zero</term>
  262. /// <description>
  263. /// <para>This instance is greater than <paramref name="r" />.</para>
  264. /// <para>-or-</para>
  265. /// <para><paramref name="r" /> is <see langword="null" />.</para>
  266. /// </description>
  267. /// </item>
  268. /// </list>
  269. /// </returns>
  270. /// <remarks>
  271. /// <para>
  272. /// <paramref name="r" /> must be an instance of <see cref="Level" />
  273. /// or <see langword="null" />; otherwise, an exception is thrown.
  274. /// </para>
  275. /// </remarks>
  276. /// <exception cref="ArgumentException"><paramref name="r" /> is not a <see cref="Level" />.</exception>
  277. public int CompareTo(object r)
  278. {
  279. Level target = r as Level;
  280. if (target != null)
  281. {
  282. return Compare(this, target);
  283. }
  284. throw new ArgumentException("Parameter: r, Value: [" + r + "] is not an instance of Level");
  285. }
  286. #endregion Implementation of IComparable
  287. #region Operators
  288. /// <summary>
  289. /// Returns a value indicating whether a specified <see cref="Level" />
  290. /// is greater than another specified <see cref="Level" />.
  291. /// </summary>
  292. /// <param name="l">A <see cref="Level" /></param>
  293. /// <param name="r">A <see cref="Level" /></param>
  294. /// <returns>
  295. /// <c>true</c> if <paramref name="l" /> is greater than
  296. /// <paramref name="r" />; otherwise, <c>false</c>.
  297. /// </returns>
  298. /// <remarks>
  299. /// <para>
  300. /// Compares two levels.
  301. /// </para>
  302. /// </remarks>
  303. public static bool operator > (Level l, Level r)
  304. {
  305. return l.m_levelValue > r.m_levelValue;
  306. }
  307. /// <summary>
  308. /// Returns a value indicating whether a specified <see cref="Level" />
  309. /// is less than another specified <see cref="Level" />.
  310. /// </summary>
  311. /// <param name="l">A <see cref="Level" /></param>
  312. /// <param name="r">A <see cref="Level" /></param>
  313. /// <returns>
  314. /// <c>true</c> if <paramref name="l" /> is less than
  315. /// <paramref name="r" />; otherwise, <c>false</c>.
  316. /// </returns>
  317. /// <remarks>
  318. /// <para>
  319. /// Compares two levels.
  320. /// </para>
  321. /// </remarks>
  322. public static bool operator < (Level l, Level r)
  323. {
  324. return l.m_levelValue < r.m_levelValue;
  325. }
  326. /// <summary>
  327. /// Returns a value indicating whether a specified <see cref="Level" />
  328. /// is greater than or equal to another specified <see cref="Level" />.
  329. /// </summary>
  330. /// <param name="l">A <see cref="Level" /></param>
  331. /// <param name="r">A <see cref="Level" /></param>
  332. /// <returns>
  333. /// <c>true</c> if <paramref name="l" /> is greater than or equal to
  334. /// <paramref name="r" />; otherwise, <c>false</c>.
  335. /// </returns>
  336. /// <remarks>
  337. /// <para>
  338. /// Compares two levels.
  339. /// </para>
  340. /// </remarks>
  341. public static bool operator >= (Level l, Level r)
  342. {
  343. return l.m_levelValue >= r.m_levelValue;
  344. }
  345. /// <summary>
  346. /// Returns a value indicating whether a specified <see cref="Level" />
  347. /// is less than or equal to another specified <see cref="Level" />.
  348. /// </summary>
  349. /// <param name="l">A <see cref="Level" /></param>
  350. /// <param name="r">A <see cref="Level" /></param>
  351. /// <returns>
  352. /// <c>true</c> if <paramref name="l" /> is less than or equal to
  353. /// <paramref name="r" />; otherwise, <c>false</c>.
  354. /// </returns>
  355. /// <remarks>
  356. /// <para>
  357. /// Compares two levels.
  358. /// </para>
  359. /// </remarks>
  360. public static bool operator <= (Level l, Level r)
  361. {
  362. return l.m_levelValue <= r.m_levelValue;
  363. }
  364. /// <summary>
  365. /// Returns a value indicating whether two specified <see cref="Level" />
  366. /// objects have the same value.
  367. /// </summary>
  368. /// <param name="l">A <see cref="Level" /> or <see langword="null" />.</param>
  369. /// <param name="r">A <see cref="Level" /> or <see langword="null" />.</param>
  370. /// <returns>
  371. /// <c>true</c> if the value of <paramref name="l" /> is the same as the
  372. /// value of <paramref name="r" />; otherwise, <c>false</c>.
  373. /// </returns>
  374. /// <remarks>
  375. /// <para>
  376. /// Compares two levels.
  377. /// </para>
  378. /// </remarks>
  379. public static bool operator == (Level l, Level r)
  380. {
  381. if (((object)l) != null && ((object)r) != null)
  382. {
  383. return l.m_levelValue == r.m_levelValue;
  384. }
  385. else
  386. {
  387. return ((object) l) == ((object) r);
  388. }
  389. }
  390. /// <summary>
  391. /// Returns a value indicating whether two specified <see cref="Level" />
  392. /// objects have different values.
  393. /// </summary>
  394. /// <param name="l">A <see cref="Level" /> or <see langword="null" />.</param>
  395. /// <param name="r">A <see cref="Level" /> or <see langword="null" />.</param>
  396. /// <returns>
  397. /// <c>true</c> if the value of <paramref name="l" /> is different from
  398. /// the value of <paramref name="r" />; otherwise, <c>false</c>.
  399. /// </returns>
  400. /// <remarks>
  401. /// <para>
  402. /// Compares two levels.
  403. /// </para>
  404. /// </remarks>
  405. public static bool operator != (Level l, Level r)
  406. {
  407. return !(l==r);
  408. }
  409. #endregion Operators
  410. #region Public Static Methods
  411. /// <summary>
  412. /// Compares two specified <see cref="Level"/> instances.
  413. /// </summary>
  414. /// <param name="l">The first <see cref="Level"/> to compare.</param>
  415. /// <param name="r">The second <see cref="Level"/> to compare.</param>
  416. /// <returns>
  417. /// A 32-bit signed integer that indicates the relative order of the
  418. /// two values compared. The return value has these meanings:
  419. /// <list type="table">
  420. /// <listheader>
  421. /// <term>Value</term>
  422. /// <description>Meaning</description>
  423. /// </listheader>
  424. /// <item>
  425. /// <term>Less than zero</term>
  426. /// <description><paramref name="l" /> is less than <paramref name="r" />.</description>
  427. /// </item>
  428. /// <item>
  429. /// <term>Zero</term>
  430. /// <description><paramref name="l" /> is equal to <paramref name="r" />.</description>
  431. /// </item>
  432. /// <item>
  433. /// <term>Greater than zero</term>
  434. /// <description><paramref name="l" /> is greater than <paramref name="r" />.</description>
  435. /// </item>
  436. /// </list>
  437. /// </returns>
  438. /// <remarks>
  439. /// <para>
  440. /// Compares two levels.
  441. /// </para>
  442. /// </remarks>
  443. public static int Compare(Level l, Level r)
  444. {
  445. // Reference equals
  446. if ((object)l == (object)r)
  447. {
  448. return 0;
  449. }
  450. if (l == null && r == null)
  451. {
  452. return 0;
  453. }
  454. if (l == null)
  455. {
  456. return -1;
  457. }
  458. if (r == null)
  459. {
  460. return 1;
  461. }
  462. return l.m_levelValue.CompareTo(r.m_levelValue);
  463. }
  464. #endregion Public Static Methods
  465. #region Public Static Fields
  466. /// <summary>
  467. /// The <see cref="Off" /> level designates a higher level than all the rest.
  468. /// </summary>
  469. public readonly static Level Off = new Level(int.MaxValue, "OFF");
  470. /// <summary>
  471. /// The <see cref="Emergency" /> level designates very severe error events.
  472. /// System unusable, emergencies.
  473. /// </summary>
  474. public readonly static Level Log4Net_Debug = new Level(120000, "log4net:DEBUG");
  475. /// <summary>
  476. /// The <see cref="Emergency" /> level designates very severe error events.
  477. /// System unusable, emergencies.
  478. /// </summary>
  479. public readonly static Level Emergency = new Level(120000, "EMERGENCY");
  480. /// <summary>
  481. /// The <see cref="Fatal" /> level designates very severe error events
  482. /// that will presumably lead the application to abort.
  483. /// </summary>
  484. public readonly static Level Fatal = new Level(110000, "FATAL");
  485. /// <summary>
  486. /// The <see cref="Alert" /> level designates very severe error events.
  487. /// Take immediate action, alerts.
  488. /// </summary>
  489. public readonly static Level Alert = new Level(100000, "ALERT");
  490. /// <summary>
  491. /// The <see cref="Critical" /> level designates very severe error events.
  492. /// Critical condition, critical.
  493. /// </summary>
  494. public readonly static Level Critical = new Level(90000, "CRITICAL");
  495. /// <summary>
  496. /// The <see cref="Severe" /> level designates very severe error events.
  497. /// </summary>
  498. public readonly static Level Severe = new Level(80000, "SEVERE");
  499. /// <summary>
  500. /// The <see cref="Error" /> level designates error events that might
  501. /// still allow the application to continue running.
  502. /// </summary>
  503. public readonly static Level Error = new Level(70000, "ERROR");
  504. /// <summary>
  505. /// The <see cref="Warn" /> level designates potentially harmful
  506. /// situations.
  507. /// </summary>
  508. public readonly static Level Warn = new Level(60000, "WARN");
  509. /// <summary>
  510. /// The <see cref="Notice" /> level designates informational messages
  511. /// that highlight the progress of the application at the highest level.
  512. /// </summary>
  513. public readonly static Level Notice = new Level(50000, "NOTICE");
  514. /// <summary>
  515. /// The <see cref="Info" /> level designates informational messages that
  516. /// highlight the progress of the application at coarse-grained level.
  517. /// </summary>
  518. public readonly static Level Info = new Level(40000, "INFO");
  519. /// <summary>
  520. /// The <see cref="Debug" /> level designates fine-grained informational
  521. /// events that are most useful to debug an application.
  522. /// </summary>
  523. public readonly static Level Debug = new Level(30000, "DEBUG");
  524. /// <summary>
  525. /// The <see cref="Fine" /> level designates fine-grained informational
  526. /// events that are most useful to debug an application.
  527. /// </summary>
  528. public readonly static Level Fine = new Level(30000, "FINE");
  529. /// <summary>
  530. /// The <see cref="Trace" /> level designates fine-grained informational
  531. /// events that are most useful to debug an application.
  532. /// </summary>
  533. public readonly static Level Trace = new Level(20000, "TRACE");
  534. /// <summary>
  535. /// The <see cref="Finer" /> level designates fine-grained informational
  536. /// events that are most useful to debug an application.
  537. /// </summary>
  538. public readonly static Level Finer = new Level(20000, "FINER");
  539. /// <summary>
  540. /// The <see cref="Verbose" /> level designates fine-grained informational
  541. /// events that are most useful to debug an application.
  542. /// </summary>
  543. public readonly static Level Verbose = new Level(10000, "VERBOSE");
  544. /// <summary>
  545. /// The <see cref="Finest" /> level designates fine-grained informational
  546. /// events that are most useful to debug an application.
  547. /// </summary>
  548. public readonly static Level Finest = new Level(10000, "FINEST");
  549. /// <summary>
  550. /// The <see cref="All" /> level designates the lowest level possible.
  551. /// </summary>
  552. public readonly static Level All = new Level(int.MinValue, "ALL");
  553. #endregion Public Static Fields
  554. #region Private Instance Fields
  555. private readonly int m_levelValue;
  556. private readonly string m_levelName;
  557. private readonly string m_levelDisplayName;
  558. #endregion Private Instance Fields
  559. }
  560. }