Exemplo n.º 1
0
        /// <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="configFile">Configuration file which contains LoggerSetup info; if null or string.Empty use the default configuration file.</param>
        /// <param name="throwOnError">true to throw any exception that occurs.-or- false to ignore any exception that occurs.</param>
        /// <returns>Logger instance.</returns>
        public static Logger Open(string logFile, string configFile, bool throwOnError = false)
        {
            LogConfig logConfig = new LogConfig();

            if (!string.IsNullOrEmpty(logFile))
            {
                logConfig.LogFile = logFile;
            }

            logConfig.LoggerSetup = LogConfigManager.GetLoggerSetup(configFile, throwOnError);

            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);
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Opens the log configuration file for the current application.
        /// </summary>
        /// <param name="configFile">Configuration file which contains LogConfig info; if null or string.Empty use the default configuration file.</param>
        /// <param name="throwOnError">true to throw any exception that occurs.-or- false to ignore any exception that occurs.</param>
        /// <returns>Logger instance.</returns>
        public static Logger OpenConfig(string configFile = null, bool throwOnError = false)
        {
            LogConfig logConfig = LogConfigManager.GetLogConfig(configFile, throwOnError);

            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);
                }
            }
        }
Exemplo n.º 3
0
        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();
        }
Exemplo n.º 4
0
        /// <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;
            }
        }
Exemplo n.º 5
0
        /// <summary>
        /// Watches config file changed.
        /// </summary>
        private void WatchConfigFileChanged()
        {
            if (this._isConfigFileChanged)
            {
                this._loggerSetup = LogConfigManager.GetLoggerSetup(this._configFile);

                this.InitFileAppender();

                this._isConfigFileChanged = false;
            }
        }
Exemplo n.º 6
0
        /// <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="configFile">Configuration file which contains LoggerSetup info; if null or string.Empty use the default configuration file.</param>
        /// <returns>Logger instance.</returns>
        public static Logger Open(string logFile, string configFile)
        {
            LogConfig logConfig = new LogConfig();

            if (!string.IsNullOrEmpty(logFile))
            {
                logConfig.LogFile = logFile;
            }

            logConfig.LoggerSetup = LogConfigManager.GetLoggerSetup(configFile);

            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();
            }
        }
Exemplo n.º 7
0
 /// <summary>
 /// Serves as a hash function for LogConfig instance.
 /// </summary>
 /// <returns>A hash code for the current LogConfig.</returns>
 public override int GetHashCode()
 {
     return(string.Format(
                "{0}|{1}|{2}|{3}|{4}|{5}|{6}|{7}|{8}|{9}",
                LogConfigManager.GetFileFullPath(this.LogFile).ToLowerInvariant(),
                this.LoggerSetup.DateTimeFormat ?? string.Empty,
                this.LoggerSetup.Level.ToString(),
                this.LoggerSetup.WriteToConsole.ToString(),
                this.LoggerSetup.WriteToFile.ToString(),
                this.LoggerSetup.UseBracket.ToString(),
                this.LoggerSetup.EnableStackInfo.ToString(),
                this.LoggerSetup.RollingFileSizeLimit.ToString(),
                this.LoggerSetup.RollingFileCountLimit.ToString(),
                this.LoggerSetup.RollingByDate.ToString()).GetHashCode());
 }