public static LoggerSetup GetLoggerSetup(string configFile) { LoggerSetup result = new LoggerSetup(); if (File.Exists(configFile)) { try { XmlDocument xmlDocument = new XmlDocument(); xmlDocument.Load(configFile); XmlNode loggerSetupNode = xmlDocument.SelectSingleNode("descendant-or-self::LoggerSetup"); if (loggerSetupNode != null) { XmlSerializer xmlSerializer = new XmlSerializer(typeof(LoggerSetup)); using (StringReader stringReader = new StringReader(loggerSetupNode.OuterXml)) { result = (LoggerSetup)xmlSerializer.Deserialize(stringReader); } } } catch (Exception e) { InternalLogger.Log(e); } } return(result); }
/// <summary> /// Initializes a new instance of the <see cref="MutexMultiProcessFileAppender" /> class. /// </summary> /// <param name="filename">File to write.</param> /// <param name="loggerSetup">Log setup.</param> public MutexMultiProcessFileAppender(string filename, LoggerSetup loggerSetup) { try { this._fileName = Path.GetFullPath(filename); this._fileInfo = new FileInfo(this._fileName); this._loggerSetup = loggerSetup; this._mutex = this.CreateSharedMutex(this.GetMutexName(this._fileName)); this.OpenTime = DateTime.Now; this.LastWriteTime = DateTime.MinValue; } catch (Exception e) { if (this._mutex != null) { this._mutex.Close(); this._mutex = null; } InternalLogger.Log(e); throw; } }
/// <summary> /// Initializes a new instance of the <see cref="Logger" /> class. /// </summary> /// <param name="logFile">Log file.</param> /// <param name="loggerSetup">Log setup.</param> internal Logger(string logFile, LoggerSetup loggerSetup) { this._logFile = logFile; this._loggerSetup = loggerSetup; if (this._loggerSetup.WriteToFile) { try { this._fileAppender = new MutexMultiProcessFileAppender(this._logFile, this._loggerSetup); } catch (Exception e) { InternalLogger.Log(e); } } if (this._loggerSetup.WriteToFile && this._fileAppender != null) { this._queueWaitHandle = new AutoResetEvent(false); this._queue = new Queue <string>(); this._consumerThread = new Thread(this.ConsumerThread); this._consumerThread.IsBackground = true; this._consumerThread.Start(); } }
internal Logger(string logFile, LoggerSetup loggerSetup, string configFile) { this._logFile = logFile; this._loggerSetup = loggerSetup; this._queueWaitHandle = new AutoResetEvent(false); this._queue = new Queue <string>(); this._consumerThread = new Thread(this.ConsumerThread); this._consumerThread.IsBackground = true; this._consumerThread.Start(); if (!this.IsNullOrWhiteSpace(configFile)) { this._configFile = LogConfigManager.GetFileFullPath(configFile); try { this._configFileWatcher = new FileSystemWatcher(Path.GetDirectoryName(this._configFile), Path.GetFileName(this._configFile)); this._configFileWatcher.EnableRaisingEvents = true; this._configFileWatcher.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName; this._configFileWatcher.Changed += (s, e) => this._isConfigFileChanged = true; this._configFileWatcher.Created += (s, e) => this._isConfigFileChanged = true; this._configFileWatcher.Deleted += (s, e) => this._isConfigFileChanged = true; this._configFileWatcher.Renamed += (s, e) => this._isConfigFileChanged = true; } catch (Exception e) { InternalLogger.Log(e); } } this.InitFileAppender(); this.WatchConfigFileChanged(); }
/// <summary> /// Initializes a new instance of the <see cref="MutexMultiProcessFileAppender" /> class. /// </summary> /// <param name="filename">File to write.</param> /// <param name="loggerSetup">Log setup.</param> public MutexMultiProcessFileAppender(string filename, LoggerSetup loggerSetup) { try { this._fileName = LogConfigManager.GetFileFullPath(filename); this._directoryName = Path.GetDirectoryName(this._fileName); this._fileInfo = new FileInfo(this._fileName); this._loggerSetup = loggerSetup; this._mutex = this.CreateSharedMutex(this.GetMutexName(this._fileName)); } catch (Exception e) { if (this._mutex != null) { this._mutex.Close(); this._mutex = null; } InternalLogger.Log(e); throw; } }
/// <summary> /// Opens the log file for the current application. /// </summary> /// <param name="logFile">Log file for the current application; if null or string.Empty use the default log file.</param> /// <param name="loggerSetup">LoggerSetup info for the logger instance; if null use the default LoggerSetup info.</param> /// <returns>Logger instance.</returns> public static Logger Open(string logFile, LoggerSetup loggerSetup) { LogConfig logConfig = new LogConfig(); if (!string.IsNullOrEmpty(logFile)) { logConfig.LogFile = logFile; } if (loggerSetup != null) { logConfig.LoggerSetup = loggerSetup; } int key = logConfig.GetHashCode(); Lock.AcquireReaderLock(Timeout.Infinite); try { if (LoggerDictionary.ContainsKey(key)) { return(LoggerDictionary[key]); } else { LockCookie lockCookie = Lock.UpgradeToWriterLock(Timeout.Infinite); try { if (LoggerDictionary.ContainsKey(key)) { return(LoggerDictionary[key]); } else { Logger result = new Logger(logConfig); LoggerDictionary.Add(key, result); return(result); } } finally { Lock.DowngradeFromWriterLock(ref lockCookie); } } } finally { Lock.ReleaseReaderLock(); } }
/// <summary> /// Watches config file changed. /// </summary> private void WatchConfigFileChanged() { if (this._isConfigFileChanged) { this._loggerSetup = LogConfigManager.GetLoggerSetup(this._configFile); this.InitFileAppender(); this._isConfigFileChanged = false; } }
/// <summary> /// Initializes a new instance of the <see cref="LoggerSetup"/> class. /// </summary> /// <param name="loggerSetup">The logger setup.</param> private LoggerSetup(LoggerSetup loggerSetup) { this.DateTimeFormat = loggerSetup.DateTimeFormat; this.Level = loggerSetup.Level; this.WriteToConsole = loggerSetup.WriteToConsole; this.WriteToFile = loggerSetup.WriteToFile; this.UseBracket = loggerSetup.UseBracket; this.EnableStackInfo = loggerSetup.EnableStackInfo; this.RollingFileSizeMBLimit = loggerSetup.RollingFileSizeMBLimit; this.RollingFileCountLimit = loggerSetup.RollingFileCountLimit; this.RollingByDate = loggerSetup.RollingByDate; }
public static LoggerSetup GetLoggerSetup(string configFile, bool throwOnError = false) { if (string.IsNullOrEmpty(configFile) && throwOnError) { throw new ArgumentNullException("configFile", "The specified file name is null or empty."); } LoggerSetup result = new LoggerSetup(); if (File.Exists(configFile)) { try { XmlDocument xmlDocument = new XmlDocument(); xmlDocument.Load(configFile); XmlNode loggerSetupNode = xmlDocument.SelectSingleNode("descendant-or-self::LoggerSetup"); if (loggerSetupNode != null) { XmlSerializer xmlSerializer = new XmlSerializer(typeof(LoggerSetup)); using (StringReader stringReader = new StringReader(loggerSetupNode.OuterXml)) { result = (LoggerSetup)xmlSerializer.Deserialize(stringReader); } } } catch (Exception e) { InternalLogger.Log(e); if (throwOnError) { throw; } } } else { if (throwOnError) { throw new FileNotFoundException("The specified file does not exist.", configFile); } } return(result); }
/// <summary> /// Opens the log file for the current application. /// </summary> /// <param name="logFile">Log file for the current application; if null or string.Empty use the default log file.</param> /// <param name="loggerSetup">LoggerSetup info for the logger instance; if null use the default LoggerSetup info.</param> /// <returns>Logger instance.</returns> public static Logger Open(string logFile, LoggerSetup loggerSetup) { LogConfig logConfig = new LogConfig(); if (!string.IsNullOrEmpty(logFile)) { logConfig.LogFile = logFile; } if (loggerSetup != null) { logConfig.LoggerSetup = loggerSetup; } int key = logConfig.GetHashCode(); if (LoggerDictionary.ContainsKey(key)) { return(LoggerDictionary[key]); } lock (SyncRoot) { if (LoggerDictionary.ContainsKey(key)) { return(LoggerDictionary[key]); } else { Logger result = new Logger(logConfig); LoggerDictionary.Add(key, result); return(result); } } }