本文介绍了如何使用Log4net RolloverFileAppender在应用程序启动时强制进行过渡?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我们的应用程序中将Log4Net配置为使用带日期戳的名称和10Meg的文件大小限制.
这会在午夜以及达到10Meg限制时自动导致过渡到新文件.每次启动(或关闭)应用程序时,我还希望将日志记录移至新文件.
我可以让所有这三个行为翻滚吗?

Have Log4Net configured in our application to use a date stamped name and a 10Meg file size limit.
This automatically causes a rollover to a new file at midnight and whenever the 10Meg limit is reached.I would also like to roll over the logging to a new file each time the application is started (or closed).
Can I get all three roll over behaviours?

推荐答案

设置 appendToFile 在配置文件中为false.

Set appendToFile to false in your config file.

<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
....
  <appendToFile value="false" />
....
</appender>

要回答克雷格的评论:

如果您正确设置了StaticLogFileName和CountDirection(请参见 http: //logging.apache.org/log4net/release/sdk/log4net.Appender.RollingFileAppender.html 了解更多信息),然后按预期进行滚动.我们在应用中以编程方式在其中使用记录器进行配置,但是代码如下所示:

If you properly setup StaticLogFileName and CountDirection (see http://logging.apache.org/log4net/release/sdk/log4net.Appender.RollingFileAppender.html for more), then things roll as desired. We programmatically configure the logger in our app where we use this, but this is what the code looks like:

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()

这篇关于如何使用Log4net RolloverFileAppender在应用程序启动时强制进行过渡?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 09:59