/// <summary>
        /// Works like the CopyStream method but does a log rotation based on time.
        /// </summary>
        private void CopyStreamWithDateRotation(Stream data, string ext)
        {
            PeriodicRollingCalendar periodicRollingCalendar = new PeriodicRollingCalendar(Pattern, Period);

            periodicRollingCalendar.init();

            byte[]     buf = new byte[1024];
            FileStream w   = new FileStream(BaseLogFileName + "_" + periodicRollingCalendar.format + ext, FileMode.Append);

            while (true)
            {
                int len = data.Read(buf, 0, buf.Length);
                if (len == 0)
                {
                    break;              // EOF
                }
                if (periodicRollingCalendar.shouldRoll)
                {// rotate at the line boundary
                    int  offset = 0;
                    bool rolled = false;
                    for (int i = 0; i < len; i++)
                    {
                        if (buf[i] == 0x0A)
                        {// at the line boundary.
                            // time to rotate.
                            w.Write(buf, offset, i + 1);
                            w.Close();
                            offset = i + 1;

                            // create a new file and write everything to the new file.
                            w      = new FileStream(BaseLogFileName + "_" + periodicRollingCalendar.format + ext, FileMode.Create);
                            rolled = true;
                            if (offset < len)
                            {
                                w.Write(buf, offset, len - offset);
                                break;
                            }
                        }
                    }

                    if (!rolled)
                    {// we didn't roll - most likely as we didnt find a line boundary, so we should log what we read and roll anyway.
                        w.Write(buf, 0, len);
                        w.Close();
                        w = new FileStream(BaseLogFileName + "_" + periodicRollingCalendar.format + ext, FileMode.Create);
                    }
                }
                else
                {// typical case. write the whole thing into the current file
                    w.Write(buf, 0, len);
                }

                w.Flush();
            }
            data.Close();
            w.Close();
        }
Example #2
0
        private PeriodicityType determinePeriodicityType()
        {
            PeriodicRollingCalendar periodicRollingCalendar = new PeriodicRollingCalendar(_format, _period);
            DateTime epoch = new DateTime(1970, 1, 1);

            foreach (PeriodicityType i in VALID_ORDERED_LIST)
            {
                string r0 = epoch.ToString(_format);
                periodicRollingCalendar.periodicityType = i;

                DateTime next = periodicRollingCalendar.nextTriggeringTime(epoch, 1);
                string   r1   = next.ToString(_format);

                if (r0 != null && r1 != null && !r0.Equals(r1))
                {
                    return(i);
                }
            }
            return(PeriodicityType.ERRONEOUS);
        }
        private PeriodicityType determinePeriodicityType()
        {
            PeriodicRollingCalendar periodicRollingCalendar = new PeriodicRollingCalendar(_format, _period);
            DateTime epoch = new DateTime(1970, 1, 1);

            foreach (PeriodicityType i in VALID_ORDERED_LIST)
            {
                string r0 = epoch.ToString(_format);
                periodicRollingCalendar.periodicityType = i;

                DateTime next = periodicRollingCalendar.nextTriggeringTime(epoch, 1);
                string r1 = next.ToString(_format);

                if (r0 != null && r1 != null && !r0.Equals(r1))
                {
                    return i;
                }
            }
            return PeriodicityType.ERRONEOUS;
        }
Example #4
0
        /// <summary>
        /// Works like the CopyStream method but does a log rotation based on time.
        /// </summary>
        private void CopyStreamWithDateRotation(StreamReader reader, string ext)
        {
            PeriodicRollingCalendar periodicRollingCalendar = new PeriodicRollingCalendar(Pattern, Period);

            periodicRollingCalendar.init();

            StreamWriter writer = CreateWriter(new FileStream(BaseLogFileName + "_" + periodicRollingCalendar.format + ext, FileMode.Append));
            string?      line;

            while ((line = reader.ReadLine()) != null)
            {
                if (periodicRollingCalendar.shouldRoll)
                {
                    writer.Dispose();
                    writer = CreateWriter(new FileStream(BaseLogFileName + "_" + periodicRollingCalendar.format + ext, FileMode.Create));
                }

                writer.WriteLine(line);
            }

            reader.Dispose();
            writer.Dispose();
        }
Example #5
0
        /// <summary>
        /// Works like the CopyStream method but does a log rotation based on time.
        /// </summary>
        private void CopyStreamWithDateRotation(Stream data, string ext)
        {
            PeriodicRollingCalendar periodicRollingCalendar = new PeriodicRollingCalendar(pattern, period);
            periodicRollingCalendar.init();

            byte[] buf = new byte[1024];
            FileStream w = new FileStream(BaseLogFileName + "_" + periodicRollingCalendar.format + ext, FileMode.Create);
            while (true)
            {
                int len = data.Read(buf, 0, buf.Length);
                if (len == 0) break;    // EOF

                if (periodicRollingCalendar.shouldRoll)
                {// rotate at the line boundary
                    int offset = 0;
                    bool rolled = false;
                    for (int i = 0; i < len; i++)
                    {
                        if (buf[i] == 0x0A)
                        {// at the line boundary.
                            // time to rotate.
                            w.Write(buf, offset, i + 1);
                            w.Close();
                            offset = i + 1;

                            // create a new file.
                            w = new FileStream(BaseLogFileName + "_" + periodicRollingCalendar.format + ext, FileMode.Create);
                            rolled = true;
                        }
                    }

                    if (!rolled)
                    {// we didn't roll - most likely as we didnt find a line boundary, so we should log what we read and roll anyway.
                        w.Write(buf, 0, len);
                        w.Close();
                        w = new FileStream(BaseLogFileName + "_" + periodicRollingCalendar.format + ext, FileMode.Create);
                    }

                }
                else
                {// typical case. write the whole thing into the current file
                    w.Write(buf, 0, len);
                }

                w.Flush();
            }
            data.Close();
            w.Close();
        }