ExtLogManager.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. using log4net.Core;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Reflection;
  6. using System.Text;
  7. namespace log4net.Ext
  8. {
  9. public class ExtLogManager
  10. {
  11. #region Static Member Variables
  12. /// <summary>
  13. /// The wrapper map to use to hold the <see cref="WebLogImpl"/> objects
  14. /// </summary>
  15. private static readonly WrapperMap s_wrapperMap = new WrapperMap(new WrapperCreationHandler(WrapperCreationHandler));
  16. #endregion
  17. #region Constructor
  18. /// <summary>
  19. /// Private constructor to prevent object creation
  20. /// </summary>
  21. private ExtLogManager() { }
  22. #endregion
  23. #region Type Specific Manager Methods
  24. /// <summary>
  25. /// Returns the named logger if it exists
  26. /// </summary>
  27. /// <remarks>
  28. /// <para>If the named logger exists (in the default hierarchy) then it
  29. /// returns a reference to the logger, otherwise it returns
  30. /// <c>null</c>.</para>
  31. /// </remarks>
  32. /// <param name="name">The fully qualified logger name to look for</param>
  33. /// <returns>The logger found, or null</returns>
  34. public static IExtLog Exists(string name)
  35. {
  36. return Exists(Assembly.GetCallingAssembly(), name);
  37. }
  38. /// <summary>
  39. /// Returns the named logger if it exists
  40. /// </summary>
  41. /// <remarks>
  42. /// <para>If the named logger exists (in the specified domain) then it
  43. /// returns a reference to the logger, otherwise it returns
  44. /// <c>null</c>.</para>
  45. /// </remarks>
  46. /// <param name="domain">the domain to lookup in</param>
  47. /// <param name="name">The fully qualified logger name to look for</param>
  48. /// <returns>The logger found, or null</returns>
  49. public static IExtLog Exists(string domain, string name)
  50. {
  51. return WrapLogger(LoggerManager.Exists(domain, name));
  52. }
  53. /// <summary>
  54. /// Returns the named logger if it exists
  55. /// </summary>
  56. /// <remarks>
  57. /// <para>If the named logger exists (in the specified assembly's domain) then it
  58. /// returns a reference to the logger, otherwise it returns
  59. /// <c>null</c>.</para>
  60. /// </remarks>
  61. /// <param name="assembly">the assembly to use to lookup the domain</param>
  62. /// <param name="name">The fully qualified logger name to look for</param>
  63. /// <returns>The logger found, or null</returns>
  64. public static IExtLog Exists(Assembly assembly, string name)
  65. {
  66. return WrapLogger(LoggerManager.Exists(assembly, name));
  67. }
  68. /// <summary>
  69. /// Returns all the currently defined loggers in the default domain.
  70. /// </summary>
  71. /// <remarks>
  72. /// <para>The root logger is <b>not</b> included in the returned array.</para>
  73. /// </remarks>
  74. /// <returns>All the defined loggers</returns>
  75. public static IExtLog[] GetCurrentLoggers()
  76. {
  77. return GetCurrentLoggers(Assembly.GetCallingAssembly());
  78. }
  79. /// <summary>
  80. /// Returns all the currently defined loggers in the specified domain.
  81. /// </summary>
  82. /// <param name="domain">the domain to lookup in</param>
  83. /// <remarks>
  84. /// The root logger is <b>not</b> included in the returned array.
  85. /// </remarks>
  86. /// <returns>All the defined loggers</returns>
  87. public static IExtLog[] GetCurrentLoggers(string domain)
  88. {
  89. return WrapLoggers(LoggerManager.GetCurrentLoggers(domain));
  90. }
  91. /// <summary>
  92. /// Returns all the currently defined loggers in the specified assembly's domain.
  93. /// </summary>
  94. /// <param name="assembly">the assembly to use to lookup the domain</param>
  95. /// <remarks>
  96. /// The root logger is <b>not</b> included in the returned array.
  97. /// </remarks>
  98. /// <returns>All the defined loggers</returns>
  99. public static IExtLog[] GetCurrentLoggers(Assembly assembly)
  100. {
  101. return WrapLoggers(LoggerManager.GetCurrentLoggers(assembly));
  102. }
  103. /// <summary>
  104. /// Retrieve or create a named logger.
  105. /// </summary>
  106. /// <remarks>
  107. /// <para>Retrieve a logger named as the <paramref name="name"/>
  108. /// parameter. If the named logger already exists, then the
  109. /// existing instance will be returned. Otherwise, a new instance is
  110. /// created.</para>
  111. ///
  112. /// <para>By default, loggers do not have a set level but inherit
  113. /// it from the hierarchy. This is one of the central features of
  114. /// log4net.</para>
  115. /// </remarks>
  116. /// <param name="name">The name of the logger to retrieve.</param>
  117. /// <returns>the logger with the name specified</returns>
  118. public static IExtLog GetLogger(string name)
  119. {
  120. return GetLogger(Assembly.GetCallingAssembly(), name);
  121. }
  122. /// <summary>
  123. /// Retrieve or create a named logger.
  124. /// </summary>
  125. /// <remarks>
  126. /// <para>Retrieve a logger named as the <paramref name="name"/>
  127. /// parameter. If the named logger already exists, then the
  128. /// existing instance will be returned. Otherwise, a new instance is
  129. /// created.</para>
  130. ///
  131. /// <para>By default, loggers do not have a set level but inherit
  132. /// it from the hierarchy. This is one of the central features of
  133. /// log4net.</para>
  134. /// </remarks>
  135. /// <param name="domain">the domain to lookup in</param>
  136. /// <param name="name">The name of the logger to retrieve.</param>
  137. /// <returns>the logger with the name specified</returns>
  138. public static IExtLog GetLogger(string domain, string name)
  139. {
  140. return WrapLogger(LoggerManager.GetLogger(domain, name));
  141. }
  142. /// <summary>
  143. /// Retrieve or create a named logger.
  144. /// </summary>
  145. /// <remarks>
  146. /// <para>Retrieve a logger named as the <paramref name="name"/>
  147. /// parameter. If the named logger already exists, then the
  148. /// existing instance will be returned. Otherwise, a new instance is
  149. /// created.</para>
  150. ///
  151. /// <para>By default, loggers do not have a set level but inherit
  152. /// it from the hierarchy. This is one of the central features of
  153. /// log4net.</para>
  154. /// </remarks>
  155. /// <param name="assembly">the assembly to use to lookup the domain</param>
  156. /// <param name="name">The name of the logger to retrieve.</param>
  157. /// <returns>the logger with the name specified</returns>
  158. public static IExtLog GetLogger(Assembly assembly, string name)
  159. {
  160. return WrapLogger(LoggerManager.GetLogger(assembly, name));
  161. }
  162. /// <summary>
  163. /// Shorthand for <see cref="LogManager.GetLogger(string)"/>.
  164. /// </summary>
  165. /// <remarks>
  166. /// Get the logger for the fully qualified name of the type specified.
  167. /// </remarks>
  168. /// <param name="type">The full name of <paramref name="type"/> will
  169. /// be used as the name of the logger to retrieve.</param>
  170. /// <returns>the logger with the name specified</returns>
  171. public static IExtLog GetLogger(Type type)
  172. {
  173. return GetLogger(Assembly.GetCallingAssembly(), type.FullName);
  174. }
  175. /// <summary>
  176. /// Shorthand for <see cref="LogManager.GetLogger(string)"/>.
  177. /// </summary>
  178. /// <remarks>
  179. /// Get the logger for the fully qualified name of the type specified.
  180. /// </remarks>
  181. /// <param name="domain">the domain to lookup in</param>
  182. /// <param name="type">The full name of <paramref name="type"/> will
  183. /// be used as the name of the logger to retrieve.</param>
  184. /// <returns>the logger with the name specified</returns>
  185. public static IExtLog GetLogger(string domain, Type type)
  186. {
  187. return WrapLogger(LoggerManager.GetLogger(domain, type));
  188. }
  189. /// <summary>
  190. /// Shorthand for <see cref="LogManager.GetLogger(string)"/>.
  191. /// </summary>
  192. /// <remarks>
  193. /// Get the logger for the fully qualified name of the type specified.
  194. /// </remarks>
  195. /// <param name="assembly">the assembly to use to lookup the domain</param>
  196. /// <param name="type">The full name of <paramref name="type"/> will
  197. /// be used as the name of the logger to retrieve.</param>
  198. /// <returns>the logger with the name specified</returns>
  199. public static IExtLog GetLogger(Assembly assembly, Type type)
  200. {
  201. return WrapLogger(LoggerManager.GetLogger(assembly, type));
  202. }
  203. #endregion
  204. #region Extension Handlers
  205. /// <summary>
  206. /// Lookup the wrapper object for the logger specified
  207. /// </summary>
  208. /// <param name="logger">the logger to get the wrapper for</param>
  209. /// <returns>the wrapper for the logger specified</returns>
  210. private static IExtLog WrapLogger(ILogger logger)
  211. {
  212. return (IExtLog)s_wrapperMap.GetWrapper(logger);
  213. }
  214. /// <summary>
  215. /// Lookup the wrapper objects for the loggers specified
  216. /// </summary>
  217. /// <param name="loggers">the loggers to get the wrappers for</param>
  218. /// <returns>Lookup the wrapper objects for the loggers specified</returns>
  219. private static IExtLog[] WrapLoggers(ILogger[] loggers)
  220. {
  221. IExtLog[] results = new IExtLog[loggers.Length];
  222. for (int i = 0; i < loggers.Length; i++)
  223. {
  224. results[i] = WrapLogger(loggers[i]);
  225. }
  226. return results;
  227. }
  228. /// <summary>
  229. /// Method to create the <see cref="ILoggerWrapper"/> objects used by
  230. /// this manager.
  231. /// </summary>
  232. /// <param name="logger">The logger to wrap</param>
  233. /// <returns>The wrapper for the logger specified</returns>
  234. private static ILoggerWrapper WrapperCreationHandler(ILogger logger)
  235. {
  236. return new ExtLogImpl(logger);
  237. }
  238. #endregion
  239. }
  240. }