Esempio n. 1
0
        /// <summary>
        /// Convenience method to copy stuff from StreamReader to StreamWriter
        /// </summary>
        protected void CopyStream(Stream reader, Stream writer)
        {
            var copy = new StreamCopyOperation(reader, writer);

            while (copy.CopyLine() != 0)
            {
            }

            reader.Dispose();
            writer.Dispose();
        }
Esempio n. 2
0
        /// <summary>
        /// Works like the CopyStream method but does a log rotation.
        /// </summary>
        private void CopyStreamWithRotation(StreamReader reader, string ext)
        {
            var  writer     = new FileStream(this.BaseLogFileName + ext, FileMode.Append);
            var  copy       = new StreamCopyOperation(reader.BaseStream, writer);
            long fileLength = new FileInfo(this.BaseLogFileName + ext).Length;

            int written;

            while ((written = copy.CopyLine()) != 0)
            {
                fileLength += written;
                if (fileLength > this.SizeThreshold)
                {
                    writer.Dispose();

                    try
                    {
                        for (int j = this.FilesToKeep; j >= 2; j--)
                        {
                            string dst = this.BaseLogFileName + "." + (j - 1) + ext;
                            string src = this.BaseLogFileName + "." + (j - 2) + ext;
                            if (File.Exists(dst))
                            {
                                File.Delete(dst);
                            }

                            if (File.Exists(src))
                            {
                                File.Move(src, dst);
                            }
                        }

                        File.Move(this.BaseLogFileName + ext, this.BaseLogFileName + ".0" + ext);
                    }
                    catch (IOException e)
                    {
                        this.EventLogger.LogEvent("Failed to roll log: " + e.Message);
                    }

                    // even if the log rotation fails, create a new one, or else
                    // we'll infinitely try to roll.
                    copy.Writer = writer = new FileStream(this.BaseLogFileName + ext, FileMode.Create);
                    fileLength  = new FileInfo(this.BaseLogFileName + ext).Length;
                }
            }

            reader.Dispose();
            writer.Dispose();
        }
Esempio n. 3
0
        /// <summary>
        /// Works like the CopyStream method but does a log rotation based on time.
        /// </summary>
        private void CopyStreamWithDateRotation(StreamReader reader, string ext)
        {
            var periodicRollingCalendar = new PeriodicRollingCalendar(this.Pattern, this.Period);

            periodicRollingCalendar.Init();

            var writer = new FileStream(this.BaseLogFileName + "_" + periodicRollingCalendar.Format + ext, FileMode.Append);
            var copy   = new StreamCopyOperation(reader.BaseStream, writer);

            while (copy.CopyLine() != 0)
            {
                if (periodicRollingCalendar.ShouldRoll)
                {
                    writer.Dispose();
                    copy.Writer = writer = new FileStream(this.BaseLogFileName + "_" + periodicRollingCalendar.Format + ext, FileMode.Create);
                }
            }

            reader.Dispose();
            writer.Dispose();
        }
Esempio n. 4
0
        private void CopyStreamWithRotation(StreamReader reader, string extension)
        {
            // lock required as the timer thread and the thread that will write to the stream could try and access the file stream at the same time
            object fileLock = new();

            string baseDirectory = Path.GetDirectoryName(this.BaseLogFileName) !;
            string baseFileName  = Path.GetFileName(this.BaseLogFileName);
            string logFile       = this.BaseLogFileName + extension;

            var  writer     = new FileStream(logFile, FileMode.Append);
            var  copy       = new StreamCopyOperation(reader.BaseStream, writer);
            long fileLength = new FileInfo(logFile).Length;

            // We auto roll at time is configured then we need to create a timer and wait until time is elasped and roll the file over
            if (this.AutoRollAtTime is TimeSpan autoRollAtTime)
            {
                // Run at start
                double tickTime = this.SetupRollTimer(autoRollAtTime);
                var    timer    = new System.Timers.Timer(tickTime);
                timer.Elapsed += (s, e) =>
                {
                    try
                    {
                        timer.Stop();
                        lock (fileLock)
                        {
                            writer.Dispose();

                            var    now            = DateTime.Now.AddDays(-1);
                            int    nextFileNumber = this.GetNextFileNumber(extension, baseDirectory, baseFileName, now);
                            string nextFileName   = Path.Combine(baseDirectory, string.Format("{0}.{1}.#{2:D4}{3}", baseFileName, now.ToString(this.FilePattern), nextFileNumber, extension));
                            File.Move(logFile, nextFileName);

                            copy.Writer = writer = new FileStream(logFile, FileMode.Create);
                            fileLength  = new FileInfo(logFile).Length;
                        }

                        // Next day so check if file can be zipped
                        this.ZipFiles(baseDirectory, extension, baseFileName);
                    }
                    catch (Exception ex)
                    {
                        this.EventLogger.LogEvent($"Failed to to trigger auto roll at time event due to: {ex.Message}");
                    }
                    finally
                    {
                        // Recalculate the next interval
                        timer.Interval = this.SetupRollTimer(autoRollAtTime);
                        timer.Start();
                    }
                };
                timer.Start();
            }

            int written;

            while ((written = copy.CopyLine()) != 0)
            {
                lock (fileLock)
                {
                    fileLength += written;
                    if (fileLength > this.SizeThreshold)
                    {
                        try
                        {
                            // roll file
                            var    now            = DateTime.Now;
                            int    nextFileNumber = this.GetNextFileNumber(extension, baseDirectory, baseFileName, now);
                            string nextFileName   = Path.Combine(
                                baseDirectory,
                                string.Format("{0}.{1}.#{2:D4}{3}", baseFileName, now.ToString(this.FilePattern), nextFileNumber, extension));
                            File.Move(logFile, nextFileName);

                            // even if the log rotation fails, create a new one, or else
                            // we'll infinitely try to roll.
                            copy.Writer = writer = new FileStream(logFile, FileMode.Create);
                            fileLength  = new FileInfo(logFile).Length;
                        }
                        catch (Exception e)
                        {
                            this.EventLogger.LogEvent($"Failed to roll size time log: {e.Message}");
                        }
                    }
                }
            }

            reader.Dispose();
            writer.Dispose();
        }