我在整个项目中使用了企业库日志记录,该日志记录工作正常,但是我注意到包含日志的文件夹已开始因不再需要的旧日志而变得臃肿。我在 app.config 中的设置是:
<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"/>
目前我可以手动删除它们,但这会导致出现问题,因为我无法访问它们的存储位置。是否有一个配置可以添加到配置文件中,将它们标记为在“x”天后删除?
最佳答案
我通过将以下内容添加到监听器声明中的 app.config 来限制企业日志记录所存储的文件数量,从而修复了此问题:
maxArchivedFiles="20"
现在,日志记录最多仅保留 20 个文件,并在达到此阈值时删除最旧的文件。
我发现的另一种不涉及企业库的方法可以在这里找到: Background Worker Check For When It's Midnight?
这是通过 System.IO 完成的,以便在经过一定时间后引发事件。然后将其与删除所有文件的功能结合起来,如本答案所示:
关于file - 自动删除 Enterprise Library 5 滚动日志文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23364679/