RootLogger.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 log4net.Util;
  21. using log4net.Core;
  22. namespace log4net.Repository.Hierarchy
  23. {
  24. /// <summary>
  25. /// The <see cref="RootLogger" /> sits at the root of the logger hierarchy tree.
  26. /// </summary>
  27. /// <remarks>
  28. /// <para>
  29. /// The <see cref="RootLogger" /> is a regular <see cref="Logger" /> except
  30. /// that it provides several guarantees.
  31. /// </para>
  32. /// <para>
  33. /// First, it cannot be assigned a <c>null</c>
  34. /// level. Second, since the root logger cannot have a parent, the
  35. /// <see cref="EffectiveLevel"/> property always returns the value of the
  36. /// level field without walking the hierarchy.
  37. /// </para>
  38. /// </remarks>
  39. /// <author>Nicko Cadell</author>
  40. /// <author>Gert Driesen</author>
  41. public class RootLogger : Logger
  42. {
  43. #region Public Instance Constructors
  44. /// <summary>
  45. /// Construct a <see cref="RootLogger"/>
  46. /// </summary>
  47. /// <param name="level">The level to assign to the root logger.</param>
  48. /// <remarks>
  49. /// <para>
  50. /// Initializes a new instance of the <see cref="RootLogger" /> class with
  51. /// the specified logging level.
  52. /// </para>
  53. /// <para>
  54. /// The root logger names itself as "root". However, the root
  55. /// logger cannot be retrieved by name.
  56. /// </para>
  57. /// </remarks>
  58. public RootLogger(Level level) : base("root")
  59. {
  60. this.Level = level;
  61. }
  62. #endregion Public Instance Constructors
  63. #region Override implementation of Logger
  64. /// <summary>
  65. /// Gets the assigned level value without walking the logger hierarchy.
  66. /// </summary>
  67. /// <value>The assigned level value without walking the logger hierarchy.</value>
  68. /// <remarks>
  69. /// <para>
  70. /// Because the root logger cannot have a parent and its level
  71. /// must not be <c>null</c> this property just returns the
  72. /// value of <see cref="Logger.Level"/>.
  73. /// </para>
  74. /// </remarks>
  75. override public Level EffectiveLevel
  76. {
  77. get
  78. {
  79. return base.Level;
  80. }
  81. }
  82. /// <summary>
  83. /// Gets or sets the assigned <see cref="Level"/> for the root logger.
  84. /// </summary>
  85. /// <value>
  86. /// The <see cref="Level"/> of the root logger.
  87. /// </value>
  88. /// <remarks>
  89. /// <para>
  90. /// Setting the level of the root logger to a <c>null</c> reference
  91. /// may have catastrophic results. We prevent this here.
  92. /// </para>
  93. /// </remarks>
  94. override public Level Level
  95. {
  96. get { return base.Level; }
  97. set
  98. {
  99. if (value == null)
  100. {
  101. LogLog.Error(declaringType, "You have tried to set a null level to root.", new LogException());
  102. }
  103. else
  104. {
  105. base.Level = value;
  106. }
  107. }
  108. }
  109. #endregion Override implementation of Logger
  110. #region Private Static Fields
  111. /// <summary>
  112. /// The fully qualified type of the RootLogger class.
  113. /// </summary>
  114. /// <remarks>
  115. /// Used by the internal logger to record the Type of the
  116. /// log message.
  117. /// </remarks>
  118. private readonly static Type declaringType = typeof(RootLogger);
  119. #endregion Private Static Fields
  120. }
  121. }