문제

I have a logging application that works great, but I want to apply the ability to maintain the size of the log file - stop it from getting too large.

Ideally, I want to check the size of the file periodically, and if it's over the configured amount (5MB or something) delete from the beginning till it reaches some size, like 4MB.

From reading other questions I'm still unclear if I can update/delete a file without reading it's entire contents. My ideal situation would be:

if(filesize > 5MB)
{
    while(filesize > 4MB)
        Delete_First_X_Many_Lines(file);
}

Thankyou in advance for any pointers and direction.

도움이 되었습니까?

해결책

I would do this:

  1. Lock the log file (prevent writes).
  2. Copy the end of the log file you want to keep to a new file.
  3. Copy the new file on top of the old log file.
  4. Unlock the log file.
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top