Keeping log files under a certain size

Try using Log4Net

http://www.codeproject.com/KB/aspnet/log4net.aspx


One technique to handle this is to have two log files which are half the maximum size each. You simply rotate between the two as you reach the max size of each file. Rotating to a file causes it to be overwritten with a new file.

A logging framework such as log4net has this functionality built in.


Linux os: check out logrotate - http://www.cyberciti.biz/faq/how-do-i-rotate-log-files/

Windows os: try googling windows logrotate. for example: http://blog.arithm.com/2008/02/07/windows-log-file-rotation/


There's no easy way to strip the data from the beginning of file. So you have several options:

  1. Keep the log in several smaller log files and delete the oldest "chunks" if the total size of all log files exceeds your limit. This is similar to what you want to do, but on different level
  2. Rename the log file to "log.date" and start a new log. Similar to (1) but not an option if you have limited disk space.
  3. IF you have enough RAM and your log size is relatively small to fit in memory, you can do the following: map the whole file into memory using Memory-mapped file, then perform move operation by taking the data from the middle of the file and moving them to the beginning. Then truncate the file. This is the only way to easily strip the data from the beginning of the log file without creating a copy of it.