LogManager.cs 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827
  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.Reflection;
  21. using log4net.Core;
  22. using log4net.Repository;
  23. namespace log4net
  24. {
  25. /// <summary>
  26. /// This class is used by client applications to request logger instances.
  27. /// </summary>
  28. /// <remarks>
  29. /// <para>
  30. /// This class has static methods that are used by a client to request
  31. /// a logger instance. The <see cref="M:GetLogger(string)"/> method is
  32. /// used to retrieve a logger.
  33. /// </para>
  34. /// <para>
  35. /// See the <see cref="ILog"/> interface for more details.
  36. /// </para>
  37. /// </remarks>
  38. /// <example>Simple example of logging messages
  39. /// <code lang="C#">
  40. /// ILog log = LogManager.GetLogger("application-log");
  41. ///
  42. /// log.Info("Application Start");
  43. /// log.Debug("This is a debug message");
  44. ///
  45. /// if (log.IsDebugEnabled)
  46. /// {
  47. /// log.Debug("This is another debug message");
  48. /// }
  49. /// </code>
  50. /// </example>
  51. /// <threadsafety static="true" instance="true" />
  52. /// <seealso cref="ILog"/>
  53. /// <author>Nicko Cadell</author>
  54. /// <author>Gert Driesen</author>
  55. public sealed class LogManager
  56. {
  57. #region Private Instance Constructors
  58. /// <summary>
  59. /// Initializes a new instance of the <see cref="LogManager" /> class.
  60. /// </summary>
  61. /// <remarks>
  62. /// Uses a private access modifier to prevent instantiation of this class.
  63. /// </remarks>
  64. private LogManager()
  65. {
  66. }
  67. #endregion Private Instance Constructors
  68. #region Type Specific Manager Methods
  69. #if !NETSTANDARD1_3 // Excluded because GetCallingAssembly() is not available in CoreFX (https://github.com/dotnet/corefx/issues/2221).
  70. /// <overloads>Returns the named logger if it exists.</overloads>
  71. /// <summary>
  72. /// Returns the named logger if it exists.
  73. /// </summary>
  74. /// <remarks>
  75. /// <para>
  76. /// If the named logger exists (in the default repository) then it
  77. /// returns a reference to the logger, otherwise it returns <c>null</c>.
  78. /// </para>
  79. /// </remarks>
  80. /// <param name="name">The fully qualified logger name to look for.</param>
  81. /// <returns>The logger found, or <c>null</c> if no logger could be found.</returns>
  82. public static ILog Exists(string name)
  83. {
  84. return Exists(Assembly.GetCallingAssembly(), name);
  85. }
  86. /// <overloads>Get the currently defined loggers.</overloads>
  87. /// <summary>
  88. /// Returns all the currently defined loggers in the default repository.
  89. /// </summary>
  90. /// <remarks>
  91. /// <para>The root logger is <b>not</b> included in the returned array.</para>
  92. /// </remarks>
  93. /// <returns>All the defined loggers.</returns>
  94. public static ILog[] GetCurrentLoggers()
  95. {
  96. return GetCurrentLoggers(Assembly.GetCallingAssembly());
  97. }
  98. /// <overloads>Get or create a logger.</overloads>
  99. /// <summary>
  100. /// Retrieves or creates a named logger.
  101. /// </summary>
  102. /// <remarks>
  103. /// <para>
  104. /// Retrieves a logger named as the <paramref name="name"/>
  105. /// parameter. If the named logger already exists, then the
  106. /// existing instance will be returned. Otherwise, a new instance is
  107. /// created.
  108. /// </para>
  109. /// <para>By default, loggers do not have a set level but inherit
  110. /// it from the hierarchy. This is one of the central features of
  111. /// log4net.
  112. /// </para>
  113. /// </remarks>
  114. /// <param name="name">The name of the logger to retrieve.</param>
  115. /// <returns>The logger with the name specified.</returns>
  116. public static ILog GetLogger(string name)
  117. {
  118. return GetLogger(Assembly.GetCallingAssembly(), name);
  119. }
  120. #endif // !NETSTANDARD1_3
  121. /// <summary>
  122. /// Returns the named logger if it exists.
  123. /// </summary>
  124. /// <remarks>
  125. /// <para>
  126. /// If the named logger exists (in the specified repository) then it
  127. /// returns a reference to the logger, otherwise it returns
  128. /// <c>null</c>.
  129. /// </para>
  130. /// </remarks>
  131. /// <param name="repository">The repository to lookup in.</param>
  132. /// <param name="name">The fully qualified logger name to look for.</param>
  133. /// <returns>
  134. /// The logger found, or <c>null</c> if the logger doesn't exist in the specified
  135. /// repository.
  136. /// </returns>
  137. public static ILog Exists(string repository, string name)
  138. {
  139. return WrapLogger(LoggerManager.Exists(repository, name));
  140. }
  141. /// <summary>
  142. /// Returns the named logger if it exists.
  143. /// </summary>
  144. /// <remarks>
  145. /// <para>
  146. /// If the named logger exists (in the repository for the specified assembly) then it
  147. /// returns a reference to the logger, otherwise it returns
  148. /// <c>null</c>.
  149. /// </para>
  150. /// </remarks>
  151. /// <param name="repositoryAssembly">The assembly to use to lookup the repository.</param>
  152. /// <param name="name">The fully qualified logger name to look for.</param>
  153. /// <returns>
  154. /// The logger, or <c>null</c> if the logger doesn't exist in the specified
  155. /// assembly's repository.
  156. /// </returns>
  157. public static ILog Exists(Assembly repositoryAssembly, string name)
  158. {
  159. return WrapLogger(LoggerManager.Exists(repositoryAssembly, name));
  160. }
  161. /// <summary>
  162. /// Returns all the currently defined loggers in the specified repository.
  163. /// </summary>
  164. /// <param name="repository">The repository to lookup in.</param>
  165. /// <remarks>
  166. /// The root logger is <b>not</b> included in the returned array.
  167. /// </remarks>
  168. /// <returns>All the defined loggers.</returns>
  169. public static ILog[] GetCurrentLoggers(string repository)
  170. {
  171. return WrapLoggers(LoggerManager.GetCurrentLoggers(repository));
  172. }
  173. /// <summary>
  174. /// Returns all the currently defined loggers in the specified assembly's repository.
  175. /// </summary>
  176. /// <param name="repositoryAssembly">The assembly to use to lookup the repository.</param>
  177. /// <remarks>
  178. /// The root logger is <b>not</b> included in the returned array.
  179. /// </remarks>
  180. /// <returns>All the defined loggers.</returns>
  181. public static ILog[] GetCurrentLoggers(Assembly repositoryAssembly)
  182. {
  183. return WrapLoggers(LoggerManager.GetCurrentLoggers(repositoryAssembly));
  184. }
  185. /// <summary>
  186. /// Retrieves or creates a named logger.
  187. /// </summary>
  188. /// <remarks>
  189. /// <para>
  190. /// Retrieve a logger named as the <paramref name="name"/>
  191. /// parameter. If the named logger already exists, then the
  192. /// existing instance will be returned. Otherwise, a new instance is
  193. /// created.
  194. /// </para>
  195. /// <para>
  196. /// By default, loggers do not have a set level but inherit
  197. /// it from the hierarchy. This is one of the central features of
  198. /// log4net.
  199. /// </para>
  200. /// </remarks>
  201. /// <param name="repository">The repository to lookup in.</param>
  202. /// <param name="name">The name of the logger to retrieve.</param>
  203. /// <returns>The logger with the name specified.</returns>
  204. public static ILog GetLogger(string repository, string name)
  205. {
  206. return WrapLogger(LoggerManager.GetLogger(repository, name));
  207. }
  208. /// <summary>
  209. /// Retrieves or creates a named logger.
  210. /// </summary>
  211. /// <remarks>
  212. /// <para>
  213. /// Retrieve a logger named as the <paramref name="name"/>
  214. /// parameter. If the named logger already exists, then the
  215. /// existing instance will be returned. Otherwise, a new instance is
  216. /// created.
  217. /// </para>
  218. /// <para>
  219. /// By default, loggers do not have a set level but inherit
  220. /// it from the hierarchy. This is one of the central features of
  221. /// log4net.
  222. /// </para>
  223. /// </remarks>
  224. /// <param name="repositoryAssembly">The assembly to use to lookup the repository.</param>
  225. /// <param name="name">The name of the logger to retrieve.</param>
  226. /// <returns>The logger with the name specified.</returns>
  227. public static ILog GetLogger(Assembly repositoryAssembly, string name)
  228. {
  229. return WrapLogger(LoggerManager.GetLogger(repositoryAssembly, name));
  230. }
  231. /// <summary>
  232. /// Shorthand for <see cref="M:LogManager.GetLogger(string)"/>.
  233. /// </summary>
  234. /// <remarks>
  235. /// Get the logger for the fully qualified name of the type specified.
  236. /// </remarks>
  237. /// <param name="type">The full name of <paramref name="type"/> will be used as the name of the logger to retrieve.</param>
  238. /// <returns>The logger with the name specified.</returns>
  239. public static ILog GetLogger(Type type)
  240. {
  241. #if NETSTANDARD1_3
  242. return GetLogger(type.GetTypeInfo().Assembly, type.FullName);
  243. #else
  244. return GetLogger(Assembly.GetCallingAssembly(), type.FullName);
  245. #endif
  246. }
  247. /// <summary>
  248. /// Shorthand for <see cref="M:LogManager.GetLogger(string)"/>.
  249. /// </summary>
  250. /// <remarks>
  251. /// Gets the logger for the fully qualified name of the type specified.
  252. /// </remarks>
  253. /// <param name="repository">The repository to lookup in.</param>
  254. /// <param name="type">The full name of <paramref name="type"/> will be used as the name of the logger to retrieve.</param>
  255. /// <returns>The logger with the name specified.</returns>
  256. public static ILog GetLogger(string repository, Type type)
  257. {
  258. return WrapLogger(LoggerManager.GetLogger(repository, type));
  259. }
  260. /// <summary>
  261. /// Shorthand for <see cref="M:LogManager.GetLogger(string)"/>.
  262. /// </summary>
  263. /// <remarks>
  264. /// Gets the logger for the fully qualified name of the type specified.
  265. /// </remarks>
  266. /// <param name="repositoryAssembly">The assembly to use to lookup the repository.</param>
  267. /// <param name="type">The full name of <paramref name="type"/> will be used as the name of the logger to retrieve.</param>
  268. /// <returns>The logger with the name specified.</returns>
  269. public static ILog GetLogger(Assembly repositoryAssembly, Type type)
  270. {
  271. return WrapLogger(LoggerManager.GetLogger(repositoryAssembly, type));
  272. }
  273. #endregion Type Specific Manager Methods
  274. #region Domain & Repository Manager Methods
  275. /// <summary>
  276. /// Shuts down the log4net system.
  277. /// </summary>
  278. /// <remarks>
  279. /// <para>
  280. /// Calling this method will <b>safely</b> close and remove all
  281. /// appenders in all the loggers including root contained in all the
  282. /// default repositories.
  283. /// </para>
  284. /// <para>
  285. /// Some appenders need to be closed before the application exists.
  286. /// Otherwise, pending logging events might be lost.
  287. /// </para>
  288. /// <para>The <c>shutdown</c> method is careful to close nested
  289. /// appenders before closing regular appenders. This is allows
  290. /// configurations where a regular appender is attached to a logger
  291. /// and again to a nested appender.
  292. /// </para>
  293. /// </remarks>
  294. public static void Shutdown()
  295. {
  296. LoggerManager.Shutdown();
  297. }
  298. #if !NETSTANDARD1_3
  299. /// <overloads>Shutdown a logger repository.</overloads>
  300. /// <summary>
  301. /// Shuts down the default repository.
  302. /// </summary>
  303. /// <remarks>
  304. /// <para>
  305. /// Calling this method will <b>safely</b> close and remove all
  306. /// appenders in all the loggers including root contained in the
  307. /// default repository.
  308. /// </para>
  309. /// <para>Some appenders need to be closed before the application exists.
  310. /// Otherwise, pending logging events might be lost.
  311. /// </para>
  312. /// <para>The <c>shutdown</c> method is careful to close nested
  313. /// appenders before closing regular appenders. This is allows
  314. /// configurations where a regular appender is attached to a logger
  315. /// and again to a nested appender.
  316. /// </para>
  317. /// </remarks>
  318. public static void ShutdownRepository()
  319. {
  320. ShutdownRepository(Assembly.GetCallingAssembly());
  321. }
  322. #endif
  323. /// <summary>
  324. /// Shuts down the repository for the repository specified.
  325. /// </summary>
  326. /// <remarks>
  327. /// <para>
  328. /// Calling this method will <b>safely</b> close and remove all
  329. /// appenders in all the loggers including root contained in the
  330. /// <paramref name="repository"/> specified.
  331. /// </para>
  332. /// <para>
  333. /// Some appenders need to be closed before the application exists.
  334. /// Otherwise, pending logging events might be lost.
  335. /// </para>
  336. /// <para>The <c>shutdown</c> method is careful to close nested
  337. /// appenders before closing regular appenders. This is allows
  338. /// configurations where a regular appender is attached to a logger
  339. /// and again to a nested appender.
  340. /// </para>
  341. /// </remarks>
  342. /// <param name="repository">The repository to shutdown.</param>
  343. public static void ShutdownRepository(string repository)
  344. {
  345. LoggerManager.ShutdownRepository(repository);
  346. }
  347. /// <summary>
  348. /// Shuts down the repository specified.
  349. /// </summary>
  350. /// <remarks>
  351. /// <para>
  352. /// Calling this method will <b>safely</b> close and remove all
  353. /// appenders in all the loggers including root contained in the
  354. /// repository. The repository is looked up using
  355. /// the <paramref name="repositoryAssembly"/> specified.
  356. /// </para>
  357. /// <para>
  358. /// Some appenders need to be closed before the application exists.
  359. /// Otherwise, pending logging events might be lost.
  360. /// </para>
  361. /// <para>
  362. /// The <c>shutdown</c> method is careful to close nested
  363. /// appenders before closing regular appenders. This is allows
  364. /// configurations where a regular appender is attached to a logger
  365. /// and again to a nested appender.
  366. /// </para>
  367. /// </remarks>
  368. /// <param name="repositoryAssembly">The assembly to use to lookup the repository.</param>
  369. public static void ShutdownRepository(Assembly repositoryAssembly)
  370. {
  371. LoggerManager.ShutdownRepository(repositoryAssembly);
  372. }
  373. #if !NETSTANDARD1_3
  374. /// <overloads>Reset the configuration of a repository</overloads>
  375. /// <summary>
  376. /// Resets all values contained in this repository instance to their defaults.
  377. /// </summary>
  378. /// <remarks>
  379. /// <para>
  380. /// Resets all values contained in the repository instance to their
  381. /// defaults. This removes all appenders from all loggers, sets
  382. /// the level of all non-root loggers to <c>null</c>,
  383. /// sets their additivity flag to <c>true</c> and sets the level
  384. /// of the root logger to <see cref="Level.Debug"/>. Moreover,
  385. /// message disabling is set to its default "off" value.
  386. /// </para>
  387. /// </remarks>
  388. public static void ResetConfiguration()
  389. {
  390. ResetConfiguration(Assembly.GetCallingAssembly());
  391. }
  392. #endif
  393. /// <summary>
  394. /// Resets all values contained in this repository instance to their defaults.
  395. /// </summary>
  396. /// <remarks>
  397. /// <para>
  398. /// Reset all values contained in the repository instance to their
  399. /// defaults. This removes all appenders from all loggers, sets
  400. /// the level of all non-root loggers to <c>null</c>,
  401. /// sets their additivity flag to <c>true</c> and sets the level
  402. /// of the root logger to <see cref="Level.Debug"/>. Moreover,
  403. /// message disabling is set to its default "off" value.
  404. /// </para>
  405. /// </remarks>
  406. /// <param name="repository">The repository to reset.</param>
  407. public static void ResetConfiguration(string repository)
  408. {
  409. LoggerManager.ResetConfiguration(repository);
  410. }
  411. /// <summary>
  412. /// Resets all values contained in this repository instance to their defaults.
  413. /// </summary>
  414. /// <remarks>
  415. /// <para>
  416. /// Reset all values contained in the repository instance to their
  417. /// defaults. This removes all appenders from all loggers, sets
  418. /// the level of all non-root loggers to <c>null</c>,
  419. /// sets their additivity flag to <c>true</c> and sets the level
  420. /// of the root logger to <see cref="Level.Debug"/>. Moreover,
  421. /// message disabling is set to its default "off" value.
  422. /// </para>
  423. /// </remarks>
  424. /// <param name="repositoryAssembly">The assembly to use to lookup the repository to reset.</param>
  425. public static void ResetConfiguration(Assembly repositoryAssembly)
  426. {
  427. LoggerManager.ResetConfiguration(repositoryAssembly);
  428. }
  429. #if !NETSTANDARD1_3
  430. /// <overloads>Get the logger repository.</overloads>
  431. /// <summary>
  432. /// Returns the default <see cref="ILoggerRepository"/> instance.
  433. /// </summary>
  434. /// <remarks>
  435. /// <para>
  436. /// Gets the <see cref="ILoggerRepository"/> for the repository specified
  437. /// by the callers assembly (<see cref="M:Assembly.GetCallingAssembly()"/>).
  438. /// </para>
  439. /// </remarks>
  440. /// <returns>The <see cref="ILoggerRepository"/> instance for the default repository.</returns>
  441. [Obsolete("Use GetRepository instead of GetLoggerRepository")]
  442. public static ILoggerRepository GetLoggerRepository()
  443. {
  444. return GetRepository(Assembly.GetCallingAssembly());
  445. }
  446. #endif
  447. /// <summary>
  448. /// Returns the default <see cref="ILoggerRepository"/> instance.
  449. /// </summary>
  450. /// <returns>The default <see cref="ILoggerRepository"/> instance.</returns>
  451. /// <remarks>
  452. /// <para>
  453. /// Gets the <see cref="ILoggerRepository"/> for the repository specified
  454. /// by the <paramref name="repository"/> argument.
  455. /// </para>
  456. /// </remarks>
  457. /// <param name="repository">The repository to lookup in.</param>
  458. [Obsolete("Use GetRepository instead of GetLoggerRepository")]
  459. public static ILoggerRepository GetLoggerRepository(string repository)
  460. {
  461. return GetRepository(repository);
  462. }
  463. /// <summary>
  464. /// Returns the default <see cref="ILoggerRepository"/> instance.
  465. /// </summary>
  466. /// <returns>The default <see cref="ILoggerRepository"/> instance.</returns>
  467. /// <remarks>
  468. /// <para>
  469. /// Gets the <see cref="ILoggerRepository"/> for the repository specified
  470. /// by the <paramref name="repositoryAssembly"/> argument.
  471. /// </para>
  472. /// </remarks>
  473. /// <param name="repositoryAssembly">The assembly to use to lookup the repository.</param>
  474. [Obsolete("Use GetRepository instead of GetLoggerRepository")]
  475. public static ILoggerRepository GetLoggerRepository(Assembly repositoryAssembly)
  476. {
  477. return GetRepository(repositoryAssembly);
  478. }
  479. #if !NETSTANDARD1_3
  480. /// <overloads>Get a logger repository.</overloads>
  481. /// <summary>
  482. /// Returns the default <see cref="ILoggerRepository"/> instance.
  483. /// </summary>
  484. /// <remarks>
  485. /// <para>
  486. /// Gets the <see cref="ILoggerRepository"/> for the repository specified
  487. /// by the callers assembly (<see cref="M:Assembly.GetCallingAssembly()"/>).
  488. /// </para>
  489. /// </remarks>
  490. /// <returns>The <see cref="ILoggerRepository"/> instance for the default repository.</returns>
  491. public static ILoggerRepository GetRepository()
  492. {
  493. return GetRepository(Assembly.GetCallingAssembly());
  494. }
  495. #endif
  496. /// <summary>
  497. /// Returns the default <see cref="ILoggerRepository"/> instance.
  498. /// </summary>
  499. /// <returns>The default <see cref="ILoggerRepository"/> instance.</returns>
  500. /// <remarks>
  501. /// <para>
  502. /// Gets the <see cref="ILoggerRepository"/> for the repository specified
  503. /// by the <paramref name="repository"/> argument.
  504. /// </para>
  505. /// </remarks>
  506. /// <param name="repository">The repository to lookup in.</param>
  507. public static ILoggerRepository GetRepository(string repository)
  508. {
  509. return LoggerManager.GetRepository(repository);
  510. }
  511. /// <summary>
  512. /// Returns the default <see cref="ILoggerRepository"/> instance.
  513. /// </summary>
  514. /// <returns>The default <see cref="ILoggerRepository"/> instance.</returns>
  515. /// <remarks>
  516. /// <para>
  517. /// Gets the <see cref="ILoggerRepository"/> for the repository specified
  518. /// by the <paramref name="repositoryAssembly"/> argument.
  519. /// </para>
  520. /// </remarks>
  521. /// <param name="repositoryAssembly">The assembly to use to lookup the repository.</param>
  522. public static ILoggerRepository GetRepository(Assembly repositoryAssembly)
  523. {
  524. return LoggerManager.GetRepository(repositoryAssembly);
  525. }
  526. #if !NETSTANDARD1_3
  527. /// <overloads>Create a domain</overloads>
  528. /// <summary>
  529. /// Creates a repository with the specified repository type.
  530. /// </summary>
  531. /// <remarks>
  532. /// <para>
  533. /// <b>CreateDomain is obsolete. Use CreateRepository instead of CreateDomain.</b>
  534. /// </para>
  535. /// <para>
  536. /// The <see cref="ILoggerRepository"/> created will be associated with the repository
  537. /// specified such that a call to <see cref="M:GetRepository()"/> will return
  538. /// the same repository instance.
  539. /// </para>
  540. /// </remarks>
  541. /// <param name="repositoryType">A <see cref="Type"/> that implements <see cref="ILoggerRepository"/>
  542. /// and has a no arg constructor. An instance of this type will be created to act
  543. /// as the <see cref="ILoggerRepository"/> for the repository specified.</param>
  544. /// <returns>The <see cref="ILoggerRepository"/> created for the repository.</returns>
  545. [Obsolete("Use CreateRepository instead of CreateDomain")]
  546. public static ILoggerRepository CreateDomain(Type repositoryType)
  547. {
  548. return CreateRepository(Assembly.GetCallingAssembly(), repositoryType);
  549. }
  550. /// <overloads>Create a logger repository.</overloads>
  551. /// <summary>
  552. /// Creates a repository with the specified repository type.
  553. /// </summary>
  554. /// <param name="repositoryType">A <see cref="Type"/> that implements <see cref="ILoggerRepository"/>
  555. /// and has a no arg constructor. An instance of this type will be created to act
  556. /// as the <see cref="ILoggerRepository"/> for the repository specified.</param>
  557. /// <returns>The <see cref="ILoggerRepository"/> created for the repository.</returns>
  558. /// <remarks>
  559. /// <para>
  560. /// The <see cref="ILoggerRepository"/> created will be associated with the repository
  561. /// specified such that a call to <see cref="M:GetRepository()"/> will return
  562. /// the same repository instance.
  563. /// </para>
  564. /// </remarks>
  565. public static ILoggerRepository CreateRepository(Type repositoryType)
  566. {
  567. return CreateRepository(Assembly.GetCallingAssembly(), repositoryType);
  568. }
  569. #endif
  570. /// <summary>
  571. /// Creates a repository with the specified name.
  572. /// </summary>
  573. /// <remarks>
  574. /// <para>
  575. /// <b>CreateDomain is obsolete. Use CreateRepository instead of CreateDomain.</b>
  576. /// </para>
  577. /// <para>
  578. /// Creates the default type of <see cref="ILoggerRepository"/> which is a
  579. /// <see cref="log4net.Repository.Hierarchy.Hierarchy"/> object.
  580. /// </para>
  581. /// <para>
  582. /// The <paramref name="repository"/> name must be unique. Repositories cannot be redefined.
  583. /// An <see cref="Exception"/> will be thrown if the repository already exists.
  584. /// </para>
  585. /// </remarks>
  586. /// <param name="repository">The name of the repository, this must be unique amongst repositories.</param>
  587. /// <returns>The <see cref="ILoggerRepository"/> created for the repository.</returns>
  588. /// <exception cref="LogException">The specified repository already exists.</exception>
  589. [Obsolete("Use CreateRepository instead of CreateDomain")]
  590. public static ILoggerRepository CreateDomain(string repository)
  591. {
  592. return LoggerManager.CreateRepository(repository);
  593. }
  594. /// <summary>
  595. /// Creates a repository with the specified name.
  596. /// </summary>
  597. /// <remarks>
  598. /// <para>
  599. /// Creates the default type of <see cref="ILoggerRepository"/> which is a
  600. /// <see cref="log4net.Repository.Hierarchy.Hierarchy"/> object.
  601. /// </para>
  602. /// <para>
  603. /// The <paramref name="repository"/> name must be unique. Repositories cannot be redefined.
  604. /// An <see cref="Exception"/> will be thrown if the repository already exists.
  605. /// </para>
  606. /// </remarks>
  607. /// <param name="repository">The name of the repository, this must be unique amongst repositories.</param>
  608. /// <returns>The <see cref="ILoggerRepository"/> created for the repository.</returns>
  609. /// <exception cref="LogException">The specified repository already exists.</exception>
  610. public static ILoggerRepository CreateRepository(string repository)
  611. {
  612. return LoggerManager.CreateRepository(repository);
  613. }
  614. /// <summary>
  615. /// Creates a repository with the specified name and repository type.
  616. /// </summary>
  617. /// <remarks>
  618. /// <para>
  619. /// <b>CreateDomain is obsolete. Use CreateRepository instead of CreateDomain.</b>
  620. /// </para>
  621. /// <para>
  622. /// The <paramref name="repository"/> name must be unique. Repositories cannot be redefined.
  623. /// An <see cref="Exception"/> will be thrown if the repository already exists.
  624. /// </para>
  625. /// </remarks>
  626. /// <param name="repository">The name of the repository, this must be unique to the repository.</param>
  627. /// <param name="repositoryType">A <see cref="Type"/> that implements <see cref="ILoggerRepository"/>
  628. /// and has a no arg constructor. An instance of this type will be created to act
  629. /// as the <see cref="ILoggerRepository"/> for the repository specified.</param>
  630. /// <returns>The <see cref="ILoggerRepository"/> created for the repository.</returns>
  631. /// <exception cref="LogException">The specified repository already exists.</exception>
  632. [Obsolete("Use CreateRepository instead of CreateDomain")]
  633. public static ILoggerRepository CreateDomain(string repository, Type repositoryType)
  634. {
  635. return LoggerManager.CreateRepository(repository, repositoryType);
  636. }
  637. /// <summary>
  638. /// Creates a repository with the specified name and repository type.
  639. /// </summary>
  640. /// <remarks>
  641. /// <para>
  642. /// The <paramref name="repository"/> name must be unique. Repositories cannot be redefined.
  643. /// An <see cref="Exception"/> will be thrown if the repository already exists.
  644. /// </para>
  645. /// </remarks>
  646. /// <param name="repository">The name of the repository, this must be unique to the repository.</param>
  647. /// <param name="repositoryType">A <see cref="Type"/> that implements <see cref="ILoggerRepository"/>
  648. /// and has a no arg constructor. An instance of this type will be created to act
  649. /// as the <see cref="ILoggerRepository"/> for the repository specified.</param>
  650. /// <returns>The <see cref="ILoggerRepository"/> created for the repository.</returns>
  651. /// <exception cref="LogException">The specified repository already exists.</exception>
  652. public static ILoggerRepository CreateRepository(string repository, Type repositoryType)
  653. {
  654. return LoggerManager.CreateRepository(repository, repositoryType);
  655. }
  656. /// <summary>
  657. /// Creates a repository for the specified assembly and repository type.
  658. /// </summary>
  659. /// <remarks>
  660. /// <para>
  661. /// <b>CreateDomain is obsolete. Use CreateRepository instead of CreateDomain.</b>
  662. /// </para>
  663. /// <para>
  664. /// The <see cref="ILoggerRepository"/> created will be associated with the repository
  665. /// specified such that a call to <see cref="M:GetRepository(Assembly)"/> with the
  666. /// same assembly specified will return the same repository instance.
  667. /// </para>
  668. /// </remarks>
  669. /// <param name="repositoryAssembly">The assembly to use to get the name of the repository.</param>
  670. /// <param name="repositoryType">A <see cref="Type"/> that implements <see cref="ILoggerRepository"/>
  671. /// and has a no arg constructor. An instance of this type will be created to act
  672. /// as the <see cref="ILoggerRepository"/> for the repository specified.</param>
  673. /// <returns>The <see cref="ILoggerRepository"/> created for the repository.</returns>
  674. [Obsolete("Use CreateRepository instead of CreateDomain")]
  675. public static ILoggerRepository CreateDomain(Assembly repositoryAssembly, Type repositoryType)
  676. {
  677. return LoggerManager.CreateRepository(repositoryAssembly, repositoryType);
  678. }
  679. /// <summary>
  680. /// Creates a repository for the specified assembly and repository type.
  681. /// </summary>
  682. /// <remarks>
  683. /// <para>
  684. /// The <see cref="ILoggerRepository"/> created will be associated with the repository
  685. /// specified such that a call to <see cref="M:GetRepository(Assembly)"/> with the
  686. /// same assembly specified will return the same repository instance.
  687. /// </para>
  688. /// </remarks>
  689. /// <param name="repositoryAssembly">The assembly to use to get the name of the repository.</param>
  690. /// <param name="repositoryType">A <see cref="Type"/> that implements <see cref="ILoggerRepository"/>
  691. /// and has a no arg constructor. An instance of this type will be created to act
  692. /// as the <see cref="ILoggerRepository"/> for the repository specified.</param>
  693. /// <returns>The <see cref="ILoggerRepository"/> created for the repository.</returns>
  694. public static ILoggerRepository CreateRepository(Assembly repositoryAssembly, Type repositoryType)
  695. {
  696. return LoggerManager.CreateRepository(repositoryAssembly, repositoryType);
  697. }
  698. /// <summary>
  699. /// Gets the list of currently defined repositories.
  700. /// </summary>
  701. /// <remarks>
  702. /// <para>
  703. /// Get an array of all the <see cref="ILoggerRepository"/> objects that have been created.
  704. /// </para>
  705. /// </remarks>
  706. /// <returns>An array of all the known <see cref="ILoggerRepository"/> objects.</returns>
  707. public static ILoggerRepository[] GetAllRepositories()
  708. {
  709. return LoggerManager.GetAllRepositories();
  710. }
  711. /// <summary>
  712. /// Flushes logging events buffered in all configured appenders in the default repository.
  713. /// </summary>
  714. /// <param name="millisecondsTimeout">The maximum time in milliseconds to wait for logging events from asycnhronous appenders to be flushed.</param>
  715. /// <returns><c>True</c> if all logging events were flushed successfully, else <c>false</c>.</returns>
  716. public static bool Flush(int millisecondsTimeout)
  717. {
  718. #if !NETSTANDARD1_3 // Excluded because GetCallingAssembly() is not available in CoreFX (https://github.com/dotnet/corefx/issues/2221).
  719. Appender.IFlushable flushableRepository = LoggerManager.GetRepository(Assembly.GetCallingAssembly()) as Appender.IFlushable;
  720. if (flushableRepository == null)
  721. {
  722. return false;
  723. }
  724. else
  725. {
  726. return flushableRepository.Flush(millisecondsTimeout);
  727. }
  728. #else
  729. return false;
  730. #endif
  731. }
  732. #endregion Domain & Repository Manager Methods
  733. #region Extension Handlers
  734. /// <summary>
  735. /// Looks up the wrapper object for the logger specified.
  736. /// </summary>
  737. /// <param name="logger">The logger to get the wrapper for.</param>
  738. /// <returns>The wrapper for the logger specified.</returns>
  739. private static ILog WrapLogger(ILogger logger)
  740. {
  741. return (ILog)s_wrapperMap.GetWrapper(logger);
  742. }
  743. /// <summary>
  744. /// Looks up the wrapper objects for the loggers specified.
  745. /// </summary>
  746. /// <param name="loggers">The loggers to get the wrappers for.</param>
  747. /// <returns>The wrapper objects for the loggers specified.</returns>
  748. private static ILog[] WrapLoggers(ILogger[] loggers)
  749. {
  750. ILog[] results = new ILog[loggers.Length];
  751. for(int i=0; i<loggers.Length; i++)
  752. {
  753. results[i] = WrapLogger(loggers[i]);
  754. }
  755. return results;
  756. }
  757. /// <summary>
  758. /// Create the <see cref="ILoggerWrapper"/> objects used by
  759. /// this manager.
  760. /// </summary>
  761. /// <param name="logger">The logger to wrap.</param>
  762. /// <returns>The wrapper for the logger specified.</returns>
  763. private static ILoggerWrapper WrapperCreationHandler(ILogger logger)
  764. {
  765. return new LogImpl(logger);
  766. }
  767. #endregion
  768. #region Private Static Fields
  769. /// <summary>
  770. /// The wrapper map to use to hold the <see cref="LogImpl"/> objects.
  771. /// </summary>
  772. private static readonly WrapperMap s_wrapperMap = new WrapperMap(new WrapperCreationHandler(WrapperCreationHandler));
  773. #endregion Private Static Fields
  774. }
  775. }