Truncating flat file based on maximum log file size

Topics: Logging Application Block
Nov 19, 2014 at 11:06 PM
Hi.

Thank you for Enterprise Library 5.0.

It doesn't seem that the built-in RollingFlatFileTraceListener supports the ability to truncate the log file if it exceeds a certain size, without rolling to a new file or overwriting/clearing the existing file.

Has anyone seen this done before?

Is it even possible to do this using a custom trace listener?

Thank you.
Nov 20, 2014 at 5:39 PM
What do you mean by truncate in this context? Do you mean removing the first LogEntry from the file and then writing the new LogEntry at the bottom (or top)?

You've described the RollingFlatFileTraceListener behavior so I don't think it supports the behavior you want.

~~
Randy Levy
entlib.support@live.com
Enterprise Library support engineer
Support How-to
Nov 20, 2014 at 11:38 PM
If I specify a maximum log file size, I want old log entries (from the top) to be removed if a new log entry is added that would exceed the maximum log file size.

The old log entries that are removed do not have to be complete entries, but only the specific number of bytes required to truncate the log file enough so as not to exceed the maximum log file size.

Thank you.
Nov 21, 2014 at 1:24 AM
Typically, trace listeners using file based access are synchronized so only one write operation occurs at a time. With that in mind, if you can implement the logic you want then it should be possible to put it in a custom trace listener. There are some considerations:
  • Performance could be a consideration especially for large files
  • How do you know where an entry starts/ends (depends on the file format but maybe the formatter changed -- perhaps unlikely but a consideration)?
If you are using .NET 4+ then you might want to look at Memory-Mapped Files with random access.

~~
Randy Levy
entlib.support@live.com
Enterprise Library support engineer
Support How-to