Option to clear log file content needed in RollingFlatFileTraceListener

Topics: Logging Application Block
Jan 13, 2016 at 6:38 PM
I have a requirement to clear the contents of the current log file and can't find an option for doing that in Ent Lib v6. Tried to manually clear the contents using FileStream but got the File is already in use error
Jan 13, 2016 at 8:17 PM
Edited Jan 13, 2016 at 8:20 PM
You should be able to clear the log file. The first thing you need to do is to close the file (and release the lock) so that you can either clear the file or delete it (if the file does not exist then Enterprise Library will create a new file).

You can release the locks by using logWriter.Dispose() or Logger.Reset() if using the static facade class.

One thing to bear in mind is that if you are in a multi-threaded environment (e.g. web application) then you should ensure that no logging occurs between Disposing the LogWriter and any logging re-initialization (e.g. obtain a new LogWriter from the factory). One way to ensure that is to synchronize access to the logWriter.