using log4net; using log4net.Config; using log4net.Core; using log4net.Repository.Hierarchy; using System; using System.Collections.Generic; using System.Configuration; using System.IO; using System.Text; using KPlan.Util; namespace KPlan.Logger { public class TLogManager { public enum StateType { On, Off, AlwaysON, AlwaysOff } private const string LoggerStateKey = "LoggerState"; private static TLogManager.StateType globalState; private bool isConfigured; private string configPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "..\\..\\SyncTaskStatus\\Logger"); private string configName = "LogConfig.xml"; private bool isWatching = true; private Level OldLevel = Level.All; private TLogManager.StateType logState = TLogManager.StateType.Off; private static List loggers = new List(); public TLogManager.StateType State { get { return this.logState; } set { this.logState = value; if (this.logState == TLogManager.StateType.Off || this.logState == TLogManager.StateType.AlwaysOff) { this.OldLevel = ((Hierarchy)LogManager.GetRepository()).Root.Level; ((Hierarchy)LogManager.GetRepository()).Root.Level = Level.Off; return; } ((Hierarchy)LogManager.GetRepository()).Root.Level = this.OldLevel; } } public static TLogManager.StateType GlobalState { get { return TLogManager.globalState; } set { TLogManager.globalState = value; TLogManager.ChangeState(TLogManager.globalState); } } public bool IsOn { get { return this.logState != TLogManager.StateType.Off; } } public bool IsConfigured { get { return this.isConfigured; } } public bool IsWatching { get { return this.isWatching; } set { this.isWatching = value; this.Configure(); } } public string ConfigName { get { return this.configName; } set { this.configName = value; this.Configure(); } } public string ConfigPath { get { return this.configPath; } set { this.configPath = value; this.Configure(); } } public string ConfigFile { get { return Path.Combine(this.configPath, this.configName); } } public TLogManager() { TLogManager.loggers.Add(this); } public static void SetStateFromConfig() { //string text = ConfigurationManager.AppSettings["LoggerState"]; string text = KUtil.GetConfigValue(KConfigure.LOG_SECTION,KConfigure.LOG_SWITCH); TLogManager.GlobalState = ((!string.IsNullOrEmpty(text) && text.ToUpper() == "ON") ? TLogManager.StateType.On : TLogManager.StateType.Off); } public static TLogManager Create(string logDir) { return new TLogManager { State = TLogManager.GlobalState, ConfigPath = logDir }; } public static TLogManager Create() { //string Path = ConfigurationManager.AppSettings["LogPath"]; string Path = KUtil.GetConfigValue(KConfigure.LOG_SECTION, KConfigure.LOG_CONFIG_PATH); if (!string.IsNullOrEmpty(Path)) return TLogManager.Create(Path); else return TLogManager.Create("..\\..\\Logger"); } public TLogAgent GetLogger(string subSystem) { return new TLogAgent(this, subSystem); } public TLogAgent GetLogger(Type t) { return this.GetLogger(t.Name); } public TLogAgent GetLogger(object o) { return this.GetLogger(o.GetType()); } public void Configure() { FileInfo fileInfo = new FileInfo(this.ConfigFile); if (!fileInfo.Exists) { this.isConfigured = false; return; } if (this.IsWatching) { XmlConfigurator.ConfigureAndWatch(fileInfo); } else { XmlConfigurator.Configure(fileInfo); } this.isConfigured = true; } public void ConfigureFromString(string xml) { Stream configStream = new MemoryStream(Encoding.Default.GetBytes(xml)); XmlConfigurator.Configure(configStream); this.isWatching = false; this.isConfigured = true; } private static void ChangeState(TLogManager.StateType state) { foreach (TLogManager current in TLogManager.loggers) { if (current.State != TLogManager.StateType.AlwaysON && current.State != TLogManager.StateType.AlwaysOff) { current.State = state; } } } } }