Constant file size

  • Thread starter =?iso-8859-1?B?QWRyaeFuIEUuIEPzcmRvYmE=?=
  • Start date
?

=?iso-8859-1?B?QWRyaeFuIEUuIEPzcmRvYmE=?=

Hi, there!
I'm developing a little application which must record some events in a
log file (.txt).
How can I delete the first line in the log file every time I add a new
line at the end, in order to keep constant the file size?

Thank you in advance.
 
A

Andy

If you need logging, I suggest looking at the log4net project, found at
the Apache Foundations site. They have a LogFileAppender, which has
the functionality you describe.

HTH
Andy
 
?

=?iso-8859-1?B?QWRyaeFuIEUuIEPzcmRvYmE=?=

Sorry, Andy. But I think LogFileAppender always add new lines to the
log file, so the file increase its size.
I need to keep constant the size of the log file.

Thank you, for your comments.
 
J

Jon Skeet [C# MVP]

Adrián E. Córdoba said:
I'm developing a little application which must record some events in a
log file (.txt).
How can I delete the first line in the log file every time I add a new
line at the end, in order to keep constant the file size?

Two things:

1) Keeping the same number of lines *won't* keep the file the same
size, unless all lines are the same size

2) Deleting the first line in a file involves rewriting the whole file.
If you use a fixed-size encoding (eg Encoding.Unicode) and keep the
lines the same size, you can make it a rolling log file, writing into
the middle of the file, overwriting lines on a rolling basis.
 
?

=?iso-8859-1?B?QWRyaeFuIEUuIEPzcmRvYmE=?=

Thank you for your comments, Jon.

1) It is not necessary the file size will be exactly XX MB. It's enough
to keep constant the number of lines.

2) Working with a "rolling log file" and writting in the middle of the
file, make the file hard to read if you open the file with simple text
applications like Notepad because you need to find the oldest line in
the file. It is possible but it isn't desirable.

Thank you, again.
 
J

Jon Skeet [C# MVP]

Adrián E. Córdoba said:
Thank you for your comments, Jon.

1) It is not necessary the file size will be exactly XX MB. It's enough
to keep constant the number of lines.
Okay.

2) Working with a "rolling log file" and writting in the middle of the
file, make the file hard to read if you open the file with simple text
applications like Notepad because you need to find the oldest line in
the file. It is possible but it isn't desirable.

In that case, unless you *really* want to rewrite the whole log file
each time, I suggest you roll over to different log files (deleting old
ones) rather than try to keep it all in one log.

Jon
 
A

Andy

Just to add to what Jon said, if you do go the rolling log method,
log4net does support that natively. No need to write your own.

Andy
 
?

=?iso-8859-1?B?QWRyaeFuIEUuIEPzcmRvYmE=?=

Thanks to all.

--
Adrián

Andy ha escrito:
Just to add to what Jon said, if you do go the rolling log method,
log4net does support that natively. No need to write your own.

Andy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top