我有以下配置,但我无法找到有关如何在日期滚动样式上设置最大备份文件的任何文档.我知道您可以使用maxSizeRollBackups以大小滚动样式执行此操作.
Chasler.. 46
你不能.
来自
log4net SDK参考
RollingFileAppender类
警告
不支持在日期/时间边界上滚动时的最大备份文件数.
这不是maxSizeRollBackups所做的,如果日期的文件被锁定或太大,则会为同一日期创建备份.这限制了那些,而不是保存了多少"天"的文件,只有一天的多个文件. (4认同)
使用maxSizeRollBackups更新它:http://logging.apache.org/log4net/release/sdk/log4net.Appender.RollingFileAppender.MaxSizeRollBackups.html (3认同)
而且,RollingFileAppender类文档中仍然存在警告:https://logging.apache.org/log4net/release/sdk/html/T_log4net_Appender_RollingFileAppender.htm (2认同)
Jeff.. 39
即使它不受支持,这是我处理这种情况的方式:
这是我的配置:
在应用程序启动时我做:
XmlConfigurator.Configure(); var date = DateTime.Now.AddDays(-10); var task = new LogFileCleanupTask(); task.CleanUp(date);
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using log4net; using log4net.Appender; using log4net.Config; public class LogFileCleanupTask { #region - Constructor - public LogFileCleanupTask() { } #endregion #region - Methods - ////// Cleans up. Auto configures the cleanup based on the log4net configuration /// /// Anything prior will not be kept. public void CleanUp(DateTime date) { string directory = string.Empty; string filePrefix = string.Empty; var repo = LogManager.GetAllRepositories().FirstOrDefault(); ; if (repo == null) throw new NotSupportedException("Log4Net has not been configured yet."); var app = repo.GetAppenders().Where(x => x.GetType() == typeof(RollingFileAppender)).FirstOrDefault(); if (app != null) { var appender = app as RollingFileAppender; directory = Path.GetDirectoryName(appender.File); filePrefix = Path.GetFileName(appender.File); CleanUp(directory, filePrefix, date); } } ////// Cleans up. /// /// The log directory. /// The log prefix. Example: logfile dont include the file extension. /// Anything prior will not be kept. public void CleanUp(string logDirectory, string logPrefix, DateTime date) { if (string.IsNullOrEmpty(logDirectory)) throw new ArgumentException("logDirectory is missing"); if (string.IsNullOrEmpty(logPrefix)) throw new ArgumentException("logPrefix is missing"); var dirInfo = new DirectoryInfo(logDirectory); if (!dirInfo.Exists) return; var fileInfos = dirInfo.GetFiles("{0}*.*".Sub(logPrefix)); if (fileInfos.Length == 0) return; foreach (var info in fileInfos) { if (info.CreationTime < date) { info.Delete(); } } } #endregion }
Sub方法是一个扩展方法,它基本上包装string.format,如下所示:
////// Extension helper methods for strings /// [DebuggerStepThrough, DebuggerNonUserCode] public static class StringExtensions { ////// Formats a string using the /// The format. /// The args. ///and . /// A string with the format placeholders replaced by the args. public static string Sub(this string format, params object[] args) { return string.Format(format, args); } }
值得注意的是,"创建日期"可能因此而不可靠 - http://stackoverflow.com/questions/2109152/unbelievable-strange-file-creation-time-problem (3认同)
Mafu Josh.. 11
几个月前我花了一些时间研究这个问题.v1.2.10不支持基于按日期滚动删除较旧的日志文件.它位于下一版本的任务列表中.我自己获取了源代码并添加了功能,如果他们感兴趣,可以将其发布给其他人.问题和补丁可以在https://issues.apache.org/jira/browse/LOG4NET-27找到.
你不能.
来自
log4net SDK参考
RollingFileAppender类
警告
不支持在日期/时间边界上滚动时的最大备份文件数.
即使它不受支持,这是我处理这种情况的方式:
这是我的配置:
在应用程序启动时我做:
XmlConfigurator.Configure(); var date = DateTime.Now.AddDays(-10); var task = new LogFileCleanupTask(); task.CleanUp(date);
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using log4net; using log4net.Appender; using log4net.Config; public class LogFileCleanupTask { #region - Constructor - public LogFileCleanupTask() { } #endregion #region - Methods - ////// Cleans up. Auto configures the cleanup based on the log4net configuration /// /// Anything prior will not be kept. public void CleanUp(DateTime date) { string directory = string.Empty; string filePrefix = string.Empty; var repo = LogManager.GetAllRepositories().FirstOrDefault(); ; if (repo == null) throw new NotSupportedException("Log4Net has not been configured yet."); var app = repo.GetAppenders().Where(x => x.GetType() == typeof(RollingFileAppender)).FirstOrDefault(); if (app != null) { var appender = app as RollingFileAppender; directory = Path.GetDirectoryName(appender.File); filePrefix = Path.GetFileName(appender.File); CleanUp(directory, filePrefix, date); } } ////// Cleans up. /// /// The log directory. /// The log prefix. Example: logfile dont include the file extension. /// Anything prior will not be kept. public void CleanUp(string logDirectory, string logPrefix, DateTime date) { if (string.IsNullOrEmpty(logDirectory)) throw new ArgumentException("logDirectory is missing"); if (string.IsNullOrEmpty(logPrefix)) throw new ArgumentException("logPrefix is missing"); var dirInfo = new DirectoryInfo(logDirectory); if (!dirInfo.Exists) return; var fileInfos = dirInfo.GetFiles("{0}*.*".Sub(logPrefix)); if (fileInfos.Length == 0) return; foreach (var info in fileInfos) { if (info.CreationTime < date) { info.Delete(); } } } #endregion }
Sub方法是一个扩展方法,它基本上包装string.format,如下所示:
////// Extension helper methods for strings /// [DebuggerStepThrough, DebuggerNonUserCode] public static class StringExtensions { ////// Formats a string using the /// The format. /// The args. ///and . /// A string with the format placeholders replaced by the args. public static string Sub(this string format, params object[] args) { return string.Format(format, args); } }
几个月前我花了一些时间研究这个问题.v1.2.10不支持基于按日期滚动删除较旧的日志文件.它位于下一版本的任务列表中.我自己获取了源代码并添加了功能,如果他们感兴趣,可以将其发布给其他人.问题和补丁可以在https://issues.apache.org/jira/browse/LOG4NET-27找到.
要限制日志数量,请不要在日期模式中包含年份或月份,例如datePattern value ="_ dd'.log""
这将每天创建一个新日志,并在下个月被覆盖.