在我们的应用程序中配置Log4Net以使用带日期戳的名称和10Meg文件大小限制 这会在午夜和达到10Meg限制时自动导致翻转到新文件。 我还想在每次启动(或关闭)应用程序时将日志记录翻转到新文件 我可以获得所有三种滚动行为吗?
答案 0 :(得分:4)
在配置文件中将appendToFile设置为false。
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
....
<appendToFile value="false" />
....
</appender>
编辑:回答克雷格的评论:
如果您正确设置了StaticLogFileName和CountDirection(有关详细信息,请参阅http://logging.apache.org/log4net/release/sdk/log4net.Appender.RollingFileAppender.html),然后根据需要滚动。我们以编程方式在我们使用它的应用程序中配置记录器,但这就是代码的样子:
Dim Layout As New PatternLayout("%date{yyyy-MM-dd HH:mm:ss,fff} [%-6thread] %-5level %type{2}.%method(%line) - %message%newline")
Dim Appender As New log4net.Appender.RollingFileAppender()
Appender.File = Path.Combine(FileSystemHelper.LogDirectory, LogFileName)
Appender.Layout = Layout
Appender.AppendToFile = False ' we will start a new one when the program starts'
Appender.Name = "RollingLogFileAppender"
Appender.Threshold = LogLevel() 'May want to set this by configuration'
Appender.RollingStyle = log4net.Appender.RollingFileAppender.RollingMode.Size 'This means it will start a new log file each time the log grows to 10Mb'
Appender.MaximumFileSize = "10MB"
Appender.MaxSizeRollBackups = -1 'keep an infinite number of logs'
Appender.StaticLogFileName = True
Appender.CountDirection = 1 ' to reduce rollover costs'
log4net.Config.BasicConfigurator.Configure(Appender)
Appender.ActivateOptions()