using log4net.Core; using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Text; namespace log4net.Ext { public class ExtLogManager { #region Static Member Variables /// /// The wrapper map to use to hold the objects /// private static readonly WrapperMap s_wrapperMap = new WrapperMap(new WrapperCreationHandler(WrapperCreationHandler)); #endregion #region Constructor /// /// Private constructor to prevent object creation /// private ExtLogManager() { } #endregion #region Type Specific Manager Methods /// /// Returns the named logger if it exists /// /// /// If the named logger exists (in the default hierarchy) then it /// returns a reference to the logger, otherwise it returns /// null. /// /// The fully qualified logger name to look for /// The logger found, or null public static IExtLog Exists(string name) { return Exists(Assembly.GetCallingAssembly(), name); } /// /// Returns the named logger if it exists /// /// /// If the named logger exists (in the specified domain) then it /// returns a reference to the logger, otherwise it returns /// null. /// /// the domain to lookup in /// The fully qualified logger name to look for /// The logger found, or null public static IExtLog Exists(string domain, string name) { return WrapLogger(LoggerManager.Exists(domain, name)); } /// /// Returns the named logger if it exists /// /// /// If the named logger exists (in the specified assembly's domain) then it /// returns a reference to the logger, otherwise it returns /// null. /// /// the assembly to use to lookup the domain /// The fully qualified logger name to look for /// The logger found, or null public static IExtLog Exists(Assembly assembly, string name) { return WrapLogger(LoggerManager.Exists(assembly, name)); } /// /// Returns all the currently defined loggers in the default domain. /// /// /// The root logger is not included in the returned array. /// /// All the defined loggers public static IExtLog[] GetCurrentLoggers() { return GetCurrentLoggers(Assembly.GetCallingAssembly()); } /// /// Returns all the currently defined loggers in the specified domain. /// /// the domain to lookup in /// /// The root logger is not included in the returned array. /// /// All the defined loggers public static IExtLog[] GetCurrentLoggers(string domain) { return WrapLoggers(LoggerManager.GetCurrentLoggers(domain)); } /// /// Returns all the currently defined loggers in the specified assembly's domain. /// /// the assembly to use to lookup the domain /// /// The root logger is not included in the returned array. /// /// All the defined loggers public static IExtLog[] GetCurrentLoggers(Assembly assembly) { return WrapLoggers(LoggerManager.GetCurrentLoggers(assembly)); } /// /// Retrieve or create a named logger. /// /// /// Retrieve a logger named as the /// parameter. If the named logger already exists, then the /// existing instance will be returned. Otherwise, a new instance is /// created. /// /// By default, loggers do not have a set level but inherit /// it from the hierarchy. This is one of the central features of /// log4net. /// /// The name of the logger to retrieve. /// the logger with the name specified public static IExtLog GetLogger(string name) { return GetLogger(Assembly.GetCallingAssembly(), name); } /// /// Retrieve or create a named logger. /// /// /// Retrieve a logger named as the /// parameter. If the named logger already exists, then the /// existing instance will be returned. Otherwise, a new instance is /// created. /// /// By default, loggers do not have a set level but inherit /// it from the hierarchy. This is one of the central features of /// log4net. /// /// the domain to lookup in /// The name of the logger to retrieve. /// the logger with the name specified public static IExtLog GetLogger(string domain, string name) { return WrapLogger(LoggerManager.GetLogger(domain, name)); } /// /// Retrieve or create a named logger. /// /// /// Retrieve a logger named as the /// parameter. If the named logger already exists, then the /// existing instance will be returned. Otherwise, a new instance is /// created. /// /// By default, loggers do not have a set level but inherit /// it from the hierarchy. This is one of the central features of /// log4net. /// /// the assembly to use to lookup the domain /// The name of the logger to retrieve. /// the logger with the name specified public static IExtLog GetLogger(Assembly assembly, string name) { return WrapLogger(LoggerManager.GetLogger(assembly, name)); } /// /// Shorthand for . /// /// /// Get the logger for the fully qualified name of the type specified. /// /// The full name of will /// be used as the name of the logger to retrieve. /// the logger with the name specified public static IExtLog GetLogger(Type type) { return GetLogger(Assembly.GetCallingAssembly(), type.FullName); } /// /// Shorthand for . /// /// /// Get the logger for the fully qualified name of the type specified. /// /// the domain to lookup in /// The full name of will /// be used as the name of the logger to retrieve. /// the logger with the name specified public static IExtLog GetLogger(string domain, Type type) { return WrapLogger(LoggerManager.GetLogger(domain, type)); } /// /// Shorthand for . /// /// /// Get the logger for the fully qualified name of the type specified. /// /// the assembly to use to lookup the domain /// The full name of will /// be used as the name of the logger to retrieve. /// the logger with the name specified public static IExtLog GetLogger(Assembly assembly, Type type) { return WrapLogger(LoggerManager.GetLogger(assembly, type)); } #endregion #region Extension Handlers /// /// Lookup the wrapper object for the logger specified /// /// the logger to get the wrapper for /// the wrapper for the logger specified private static IExtLog WrapLogger(ILogger logger) { return (IExtLog)s_wrapperMap.GetWrapper(logger); } /// /// Lookup the wrapper objects for the loggers specified /// /// the loggers to get the wrappers for /// Lookup the wrapper objects for the loggers specified private static IExtLog[] WrapLoggers(ILogger[] loggers) { IExtLog[] results = new IExtLog[loggers.Length]; for (int i = 0; i < loggers.Length; i++) { results[i] = WrapLogger(loggers[i]); } return results; } /// /// Method to create the objects used by /// this manager. /// /// The logger to wrap /// The wrapper for the logger specified private static ILoggerWrapper WrapperCreationHandler(ILogger logger) { return new ExtLogImpl(logger); } #endregion } }