BasicConfigurator.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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.Reflection;
  22. using log4net.Appender;
  23. using log4net.Layout;
  24. using log4net.Util;
  25. using log4net.Repository;
  26. using log4net.Repository.Hierarchy;
  27. namespace log4net.Config
  28. {
  29. /// <summary>
  30. /// Use this class to quickly configure a <see cref="Hierarchy"/>.
  31. /// </summary>
  32. /// <remarks>
  33. /// <para>
  34. /// Allows very simple programmatic configuration of log4net.
  35. /// </para>
  36. /// <para>
  37. /// Only one appender can be configured using this configurator.
  38. /// The appender is set at the root of the hierarchy and all logging
  39. /// events will be delivered to that appender.
  40. /// </para>
  41. /// <para>
  42. /// Appenders can also implement the <see cref="log4net.Core.IOptionHandler"/> interface. Therefore
  43. /// they would require that the <see cref="M:log4net.Core.IOptionHandler.ActivateOptions()"/> method
  44. /// be called after the appenders properties have been configured.
  45. /// </para>
  46. /// </remarks>
  47. /// <author>Nicko Cadell</author>
  48. /// <author>Gert Driesen</author>
  49. public sealed class BasicConfigurator
  50. {
  51. #region Private Static Fields
  52. /// <summary>
  53. /// The fully qualified type of the BasicConfigurator class.
  54. /// </summary>
  55. /// <remarks>
  56. /// Used by the internal logger to record the Type of the
  57. /// log message.
  58. /// </remarks>
  59. private readonly static Type declaringType = typeof(BasicConfigurator);
  60. #endregion Private Static Fields
  61. #region Private Instance Constructors
  62. /// <summary>
  63. /// Initializes a new instance of the <see cref="BasicConfigurator" /> class.
  64. /// </summary>
  65. /// <remarks>
  66. /// <para>
  67. /// Uses a private access modifier to prevent instantiation of this class.
  68. /// </para>
  69. /// </remarks>
  70. private BasicConfigurator()
  71. {
  72. }
  73. #endregion Private Instance Constructors
  74. #region Public Static Methods
  75. #if !NETSTANDARD1_3
  76. /// <summary>
  77. /// Initializes the log4net system with a default configuration.
  78. /// </summary>
  79. /// <remarks>
  80. /// <para>
  81. /// Initializes the log4net logging system using a <see cref="ConsoleAppender"/>
  82. /// that will write to <c>Console.Out</c>. The log messages are
  83. /// formatted using the <see cref="PatternLayout"/> layout object
  84. /// with the <see cref="PatternLayout.DetailConversionPattern"/>
  85. /// layout style.
  86. /// </para>
  87. /// </remarks>
  88. static public ICollection Configure()
  89. {
  90. return BasicConfigurator.Configure(LogManager.GetRepository(Assembly.GetCallingAssembly()));
  91. }
  92. /// <summary>
  93. /// Initializes the log4net system using the specified appenders.
  94. /// </summary>
  95. /// <param name="appenders">The appenders to use to log all logging events.</param>
  96. /// <remarks>
  97. /// <para>
  98. /// Initializes the log4net system using the specified appenders.
  99. /// </para>
  100. /// </remarks>
  101. static public ICollection Configure(params IAppender[] appenders)
  102. {
  103. ArrayList configurationMessages = new ArrayList();
  104. ILoggerRepository repository = LogManager.GetRepository(Assembly.GetCallingAssembly());
  105. using (new LogLog.LogReceivedAdapter(configurationMessages))
  106. {
  107. InternalConfigure(repository, appenders);
  108. }
  109. repository.ConfigurationMessages = configurationMessages;
  110. return configurationMessages;
  111. }
  112. /// <summary>
  113. /// Initializes the log4net system using the specified appender.
  114. /// </summary>
  115. /// <param name="appender">The appender to use to log all logging events.</param>
  116. /// <remarks>
  117. /// <para>
  118. /// Initializes the log4net system using the specified appender.
  119. /// </para>
  120. /// </remarks>
  121. static public ICollection Configure(IAppender appender)
  122. {
  123. return Configure(new IAppender[] { appender });
  124. }
  125. #endif // !NETSTANDARD1_3
  126. /// <summary>
  127. /// Initializes the <see cref="ILoggerRepository"/> with a default configuration.
  128. /// </summary>
  129. /// <param name="repository">The repository to configure.</param>
  130. /// <remarks>
  131. /// <para>
  132. /// Initializes the specified repository using a <see cref="ConsoleAppender"/>
  133. /// that will write to <c>Console.Out</c>. The log messages are
  134. /// formatted using the <see cref="PatternLayout"/> layout object
  135. /// with the <see cref="PatternLayout.DetailConversionPattern"/>
  136. /// layout style.
  137. /// </para>
  138. /// </remarks>
  139. static public ICollection Configure(ILoggerRepository repository)
  140. {
  141. ArrayList configurationMessages = new ArrayList();
  142. using (new LogLog.LogReceivedAdapter(configurationMessages))
  143. {
  144. // Create the layout
  145. PatternLayout layout = new PatternLayout();
  146. layout.ConversionPattern = PatternLayout.DetailConversionPattern;
  147. layout.ActivateOptions();
  148. // Create the appender
  149. ConsoleAppender appender = new ConsoleAppender();
  150. appender.Layout = layout;
  151. appender.ActivateOptions();
  152. InternalConfigure(repository, appender);
  153. }
  154. repository.ConfigurationMessages = configurationMessages;
  155. return configurationMessages;
  156. }
  157. /// <summary>
  158. /// Initializes the <see cref="ILoggerRepository"/> using the specified appender.
  159. /// </summary>
  160. /// <param name="repository">The repository to configure.</param>
  161. /// <param name="appender">The appender to use to log all logging events.</param>
  162. /// <remarks>
  163. /// <para>
  164. /// Initializes the <see cref="ILoggerRepository"/> using the specified appender.
  165. /// </para>
  166. /// </remarks>
  167. static public ICollection Configure(ILoggerRepository repository, IAppender appender)
  168. {
  169. return Configure(repository, new IAppender[] { appender });
  170. }
  171. /// <summary>
  172. /// Initializes the <see cref="ILoggerRepository"/> using the specified appenders.
  173. /// </summary>
  174. /// <param name="repository">The repository to configure.</param>
  175. /// <param name="appenders">The appenders to use to log all logging events.</param>
  176. /// <remarks>
  177. /// <para>
  178. /// Initializes the <see cref="ILoggerRepository"/> using the specified appender.
  179. /// </para>
  180. /// </remarks>
  181. static public ICollection Configure(ILoggerRepository repository, params IAppender[] appenders)
  182. {
  183. ArrayList configurationMessages = new ArrayList();
  184. using (new LogLog.LogReceivedAdapter(configurationMessages))
  185. {
  186. InternalConfigure(repository, appenders);
  187. }
  188. repository.ConfigurationMessages = configurationMessages;
  189. return configurationMessages;
  190. }
  191. static private void InternalConfigure(ILoggerRepository repository, params IAppender[] appenders)
  192. {
  193. IBasicRepositoryConfigurator configurableRepository = repository as IBasicRepositoryConfigurator;
  194. if (configurableRepository != null)
  195. {
  196. configurableRepository.Configure(appenders);
  197. }
  198. else
  199. {
  200. LogLog.Warn(declaringType, "BasicConfigurator: Repository [" + repository + "] does not support the BasicConfigurator");
  201. }
  202. }
  203. #endregion Public Static Methods
  204. }
  205. }