/// <summary> /// 使用指定的配置元素对象创建应用程序日志对象。 /// </summary> /// <param name="config">配置元素对象。</param> /// <returns> /// 应用程序日志对象。 /// </returns> /// <remarks> /// 辅助的IAppLogger对象创建方法,与Singleton模式的静态实例变量无关。 /// </remarks> public static IAppLogger CreateInstance(AppLoggerElement config) { if (config == null) { throw new ArgumentException("Not found the special appLogger configuration element."); } if (config.LoggerType == "NullLogger") { // NullLogger对象, 忽略其余配置 return(new NullLogger(config.Name)); } else if (config.LoggerType == "AppLogger") { // AppLogger对象, 按具体配置创建 IAppLoggerImpl innerImpl = null; innerImpl = CreateImplementationObject(config.Implementation); Debug.Assert(innerImpl != null); // 创建日志实现类对象(最里层) for (int i = config.Decorators.Count - 1; i >= 0; i--) { innerImpl = CreateDecoratorObject(config.Decorators[i], innerImpl); Debug.Assert(innerImpl != null); } // 按配置顺序的逆序生成日志装饰类对象 return(new AppLogger(innerImpl)); } throw new ArgumentException("Invalid [appLogger] configuration element."); }
/// <summary> /// 释放AppLogger类对象所占用的非托管资源,也可以根据需要释放托管资源。 /// </summary> /// <param name="disposing">资源释放标志,为true则释放托管资源和非托管资源;为false则仅释放非托管资源。</param> protected virtual void Dispose(bool disposing) { if (m_innerImpl != null) { m_innerImpl.Dispose(); m_innerImpl = null; } }
/// <summary> /// 初始化ReservedCharacterDecorator类的新实例。 /// </summary> /// <param name="innerImpl">内部实现类(IAppLoggerImpl)对象。</param> /// <exception cref="System.ArgumentNullException">innerImpl参数为null时。</exception> public ReservedCharacterDecorator(IAppLoggerImpl innerImpl) : base(innerImpl) { m_reservedCharacterManager = new ReservedCharacterManager(); m_reservedCharacterManager.AddReservedCharacter((byte)0X0D, '\r', "<CR>"); m_reservedCharacterManager.AddReservedCharacter((byte)0X0A, '\n', "<LF>"); m_reservedCharacterManager.AddReservedCharacter((byte)0X1B, (char)((byte)0X1B), "<Esc>"); }
/// <summary> /// 初始化AbstractDecorator类的新实例。 /// </summary> /// <param name="innerImpl">内部实现类(IAppLoggerImpl)对象。</param> /// <exception cref="System.ArgumentNullException">innerImpl参数为null时。</exception> public AbstractDecorator(IAppLoggerImpl innerImpl) { if (innerImpl == null) { throw new ArgumentNullException("InnerImpl can not be null."); } m_innerImpl = innerImpl; }
static private IAppLogger ms_singleInstance; // Sigletone模式的唯一实例 /// <summary> /// 初始化AppLogger类的新实例。 /// </summary> /// <param name="innerImpl">内部实现类对象。</param> /// <exception cref="System.ArgumentNullException">innerImpl参数为null时。</exception> private AppLogger(IAppLoggerImpl innerImpl) { if (innerImpl == null) { throw new ArgumentNullException("Invalid IAppLoggerImpl object."); } m_innerImpl = innerImpl; }
/// <summary> /// 初始化LockDecorator类的新实例。 /// </summary> /// <param name="mutexName">Mutex对象名称。</param> /// <param name="innerImpl">内部实现类(IAppLoggerImpl)对象。</param> /// <exception cref="System.ArgumentNullException">mutexName或innerImpl参数为null或empty时。</exception> public MutexDecorator(string mutexName, IAppLoggerImpl innerImpl) : base(innerImpl) { if (String.IsNullOrEmpty(mutexName)) { throw new ArgumentNullException("MutexName can not be null."); } m_mutex = new Mutex(false, FormatMutexName(mutexName)); }
/// <summary> /// 按配置元素创建IAppLoggerImpl日志装饰类对象。 /// </summary> /// <param name="element">配置元素。</param> /// <param name="innerImpl">内部日志对象。</param> /// <returns>日志装饰类对象。</returns> /// <remarks> /// 配置元素:USe.Common.AppLogger/appLoggers/appLogger/decorators/decorator。 /// </remarks> private static IAppLoggerImpl CreateDecoratorObject(DecoratorElement element, IAppLoggerImpl innerImpl) { IAppLoggerImpl decorator = null; switch (element.LoggerType) { case "EventTypeFilter": { LogLevels level = (LogLevels)Enum.Parse(typeof(LogLevels), (string)(element.GetCustomAttribute("logLevel"))); decorator = new USe.Common.AppLogger.Decorator.EventTypeFilter(level, innerImpl); break; } case "ConsoleDecorator": { decorator = new USe.Common.AppLogger.Decorator.ConsoleDecorator(innerImpl); break; } case "LockDecorator": { decorator = new USe.Common.AppLogger.Decorator.LockDecorator(innerImpl); break; } case "MutexDecorator": { string name = element.GetCustomAttribute("mutexName") as string; if (String.IsNullOrEmpty(name)) { name = innerImpl.Name; } decorator = new USe.Common.AppLogger.Decorator.MutexDecorator(name, innerImpl); break; } case "ReservedCharacterDecorator": { decorator = new USe.Common.AppLogger.Decorator.ReservedCharacterDecorator(innerImpl); break; } default: throw new ArgumentException("Invalid [appLogger/decorators/decorator] configuration element."); } Debug.Assert(decorator != null); return(decorator); }
/// <summary> /// 按配置元素创建IAppLoggerImpl日志实现类对象。 /// </summary> /// <param name="element">配置元素。</param> /// <returns>日志实现类对象。</returns> /// <remarks> /// 配置元素:USe.Common.AppLogger/appLoggers/appLogger/implementation。 /// </remarks> private static IAppLoggerImpl CreateImplementationObject(ImplementationElement element) { USe.Common.AppLogger.EventFormatter.IEventFormatter formatter = null; IAppLoggerImpl logger = null; switch (element.EventFormatter) { case "EventStringFormatter": formatter = new USe.Common.AppLogger.EventFormatter.EventStringFormatter(); break; case "FriendlyEventStringFormatter": formatter = new USe.Common.AppLogger.EventFormatter.FriendlyEventStringFormatter(); break; default: throw new ArgumentException("Invalid [appLogger/implementation] configuration element."); } switch (element.LoggerType) { case "NullLogger": { string name = element.GetCustomAttribute("nullName") as string; if (String.IsNullOrEmpty(name)) { name = USe.Common.AppLogger.Implementation.NullLogger.GetDefaultName(); } logger = new USe.Common.AppLogger.Implementation.NullLogger(name, formatter); break; } case "ConsoleLogger": { string name = element.GetCustomAttribute("consoleName") as string; if (String.IsNullOrEmpty(name)) { name = USe.Common.AppLogger.Implementation.ConsoleLogger.GetDefaultConsoleName(); } logger = new USe.Common.AppLogger.Implementation.ConsoleLogger(name, element.Encoding, formatter); break; } case "FileLogger": { string name = element.GetCustomAttribute("fileName") as string; if (String.IsNullOrEmpty(name)) { name = USe.Common.AppLogger.Implementation.FileLogger.GetDefaultFileName(element.IsCheckUAC); } else { name = FormatFileName(name); } logger = new USe.Common.AppLogger.Implementation.FileLogger(name, element.Encoding, formatter); break; } case "FileLogger2": { string name = element.GetCustomAttribute("fileName") as string; if (String.IsNullOrEmpty(name)) { name = USe.Common.AppLogger.Implementation.FileLogger2.GetDefaultFileName(element.IsCheckUAC); } else { name = FormatFileName(name); } logger = new USe.Common.AppLogger.Implementation.FileLogger2(name, element.Encoding, formatter); break; } case "DailyFileLogger": { string name = element.GetCustomAttribute("fileName") as string; if (String.IsNullOrEmpty(name)) { name = USe.Common.AppLogger.Implementation.DailyFileLogger.GetDefaultFileName(element.IsCheckUAC); } else { name = FormatFileName(name); } logger = new USe.Common.AppLogger.Implementation.DailyFileLogger(name, element.Encoding, formatter); break; } case "DailyFileLogger2": { string name = element.GetCustomAttribute("fileName") as string; if (String.IsNullOrEmpty(name)) { name = USe.Common.AppLogger.Implementation.DailyFileLogger2.GetDefaultFileName(element.IsCheckUAC); } else { name = FormatFileName(name); } logger = new USe.Common.AppLogger.Implementation.DailyFileLogger2(name, element.Encoding, formatter); break; } default: throw new ArgumentException("Invalid [appLogger/implementation] configuration element."); } Debug.Assert(logger != null); return(logger); }
/// <summary> /// 初始化EventTypeFilter类的新实例。 /// </summary> /// <param name="logLevel">过滤日志信息的级别。</param> /// <param name="innerImpl">内部实现类(IAppLoggerImpl)对象。</param> /// <exception cref="System.ArgumentNullException">innerImpl参数为null时。</exception> public EventTypeFilter(LogLevels logLevel, IAppLoggerImpl innerImpl) : base(innerImpl) { m_logLevel = logLevel; }
/// <summary> /// 初始化ConsoleDecorator类的新实例。 /// </summary> /// <param name="innerImpl">内部实现类(IAppLoggerImpl)对象。</param> /// <exception cref="System.ArgumentNullException">innerImpl参数为null时。</exception> public ConsoleDecorator(IAppLoggerImpl innerImpl) : base(innerImpl) { m_consoleLogger = new ConsoleLogger("ConsoleDecorator", innerImpl.Encoding, new FriendlyEventStringFormatter("==>", "[HH:mm:ss.fff] ")); }
/// <summary> /// 初始化LockDecorator类的新实例。 /// </summary> /// <param name="innerImpl">内部实现类(IAppLoggerImpl)对象。</param> /// <exception cref="System.ArgumentNullException">innerImpl参数为null时。</exception> public LockDecorator(IAppLoggerImpl innerImpl) : base(innerImpl) { }