Frage

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.

War es hilfreich?

Lösung

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.
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top