Ejemplo n.º 1
0
        private void WriteTimer_Elapsed(object sender, EventArgs e)
        {
            if (timerRunning)
            {
                return;
            }

            if (queue.Count <= 0)
            {
                return;
            }

            timerRunning = true;
            while (queue.Count > 0)
            {
                try
                {
                    string filename = Path.Combine(outputDir,
                                                   this.BaseDir,
                                                   UtilFunctions.PathFix(DateTime.Now.ToString(Pattern)));

                    string dirname = Path.GetDirectoryName(filename);
                    if (!Directory.Exists(dirname))
                    {
                        Directory.CreateDirectory(dirname);
                    }

                    using (var writer = new StreamWriter(filename, true, this.Encoding, 1048576))  // 1MB
                    {
                        while (queue.Count > 0)
                        {
                            if (!queue.TryDequeue(out LogEntry entry))
                            {
                                break;
                            }

                            if (entry == null)
                            {
                                continue;
                            }

                            if (entry.ToJsonString() != null)
                            {
                                writer.WriteLine(entry.ToJsonString());
                            }
                        }
                    }
                }
                catch (IOException)
                { return; }
                catch (Exception ex)
                {
                    LogLog.Error("Throw an exception while writing log entries.", ex);
                    return;
                }
                finally
                { timerRunning = false; }
            }
        }
Ejemplo n.º 2
0
        public override void Initialize(NameValueCollection options)
        {
            if (options == null)
            {
                return;
            }

            if (!string.IsNullOrWhiteSpace(options["basedir"]))
            {
                this.BaseDir = UtilFunctions.PathFix(options["basedir"].Trim());
            }

            if (!string.IsNullOrWhiteSpace(options["pattern"]))
            {
                this.Pattern = options["pattern"].Trim();
            }

            if (!string.IsNullOrWhiteSpace(options["encoding"]))
            {
                this.Encoding = Encoding.GetEncoding(options["encoding"].Trim());
            }

            if (!string.IsNullOrWhiteSpace(options["interval"]))
            {
                if (!int.TryParse(options["interval"].Trim(), out int interval))
                {
                    interval = 200;
                }

                if (interval <= 0)
                {
                    interval = 200;
                }

                this.WriteInterval = interval;
                timer.Interval     = interval;
            }

            if (!string.IsNullOrWhiteSpace(options["max_queue_size"]))
            {
                if (!int.TryParse(options["max_queue_size"].Trim(), out int maxQueueSize))
                {
                    maxQueueSize = 10000;
                }

                if (maxQueueSize <= 0)
                {
                    maxQueueSize = 10000;
                }

                this.MaxQueueSize = maxQueueSize;
            }
        }
Ejemplo n.º 3
0
        public JsonFileConfig(string fileName, bool watchChanged = true)
        {
            this.ConfigFile = new FileInfo(UtilFunctions.PathFix(fileName));
            if (!this.ConfigFile.Exists)
            {
                throw new FileNotFoundException("File cannot be found.", this.ConfigFile.FullName);
            }

            if (watchChanged)
            {
                this.watcher                     = new FileSystemWatcher(this.ConfigFile.DirectoryName, this.ConfigFile.Name);
                this.watcher.Changed            += ConfigFileWatcher_Changed;
                this.watcher.EnableRaisingEvents = true;
            }
        }