fileloggingfile-ioenterprise-libraryenterprise-library-5

Automatically deleting Enterprise Library 5 Rolling Log files


I have Enterprise Library Logging used throughout my project which is working perfectly, however I have noticed that the folder containing the logs has started to become bloated with old logs that are no longer needed. My set up in app.config is:

    <add name="CommonListener" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
    listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.RollingFlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
    fileName="D:\Logs\Common.log" footer="   "
    formatter="Text Formatter" header="   " rollFileExistsBehavior="Increment"
    rollInterval="Midnight" rollSizeKB="1000000"/>

At present I am able to delete these manually, but that will lead to problems down the line as I won't have access to where they will be stored. Is there a configuration that I can add into the config file that will flag them to be deleted after 'x' amount of days?


Solution

  • I fixed this by limiting the amount of files that Enterprise Logging sotes by adding the following to app.config within the listener declaration:

    maxArchivedFiles="20"
    

    Now the logging will only keep up to 20 files and delete the oldest as this threshold is reached.

    Another way I found to do this which does not involve Enterprise Library can be found here: Background Worker Check For When It's Midnight?

    This done through System.IO to raise an event when a certain time has elapsed. It is then combined with a function to delete all files as found in this answer:

    delete folder/files and subfolder