By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,677 Members | 1,237 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,677 IT Pros & Developers. It's quick & easy.

Constant file size

P: n/a
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.
--
Adrián E. Córdoba

Aug 17 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a
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
Adrián E. Córdoba wrote:
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.
--
Adrián E. Córdoba
Aug 17 '06 #2

P: n/a
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.
--
Adrián

Andy ha escrito:
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
Adrián E. Córdoba wrote:
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.
--
Adrián E. Córdoba
Aug 17 '06 #3

P: n/a
Adrián E. Córdoba <so***********@gmail.comwrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Aug 17 '06 #4

P: n/a
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.

--
Adrián
Jon ha escrito:
Adrián E. Córdoba <so***********@gmail.comwrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Aug 18 '06 #5

P: n/a
Adrián E. Córdoba wrote:
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

Aug 18 '06 #6

P: n/a
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

Jon Skeet [C# MVP] wrote:
Adrián E. Córdoba wrote:
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
Aug 18 '06 #7

P: n/a
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

Jon Skeet [C# MVP] wrote:
Adrián E. Córdoba wrote:
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
Aug 18 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.