/// <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(); }
/// <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(); }
/// <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(); }