Log4Net 中 LogFileAppender 的最大文件大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/641226/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Maximum Filesize of LogFileAppender in Log4Net
提问by Ash
I am using Log4net for a while now and it's an amazing logging framework, especially when hooked into Castle.Windsor. However...
我现在使用 Log4net 有一段时间了,它是一个了不起的日志记录框架,尤其是在连接到 Castle.Windsor 时。然而...
I usually use the rolling file appender, but this has resulted in too many log files than I actually want, so instead, for my latest project, have used the basic LogFileAppender instead, but the problem is the log file keeps growing (seemingly forever).
我通常使用滚动文件附加程序,但这导致日志文件比我实际想要的多,因此,对于我的最新项目,使用了基本的 LogFileAppender,但问题是日志文件不断增长(似乎永远) .
How can I tell the appender to not go over a fixed size (and start removing old logs and appending the new ones to the file?
如何告诉附加程序不要超过固定大小(并开始删除旧日志并将新日志附加到文件中?
My current configuration looks like:
我当前的配置看起来像:
<appender name="LogFileAppender" type="log4net.Appender.FileAppender">
<file value="E:\Logs\iWater\Schedule-Dispatch-API.log"/>
<param name="AppendToFile" value="true"/>
<maximumFileSize value="2048KB"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%-16date{dd MMM HH:mm:ss} %-7level %-25.35logger{1} %message%newline"/>
</layout>
</appender>
Its seems like the maximumFileSize attribute is not being respected. Any solutions?
它似乎没有遵守 maximumFileSize 属性。任何解决方案?
Alternatively, how can I configure the rolling file appender to only create 1 file (ever)?
或者,如何将滚动文件附加程序配置为仅创建 1 个文件(永远)?
回答by Andy White
The FileAppender class does not have the MaxFileSize/MaximumFileSize properties. You only get those if you use a RollingFileAppender. Here's an example that will limit your file to a fixed maximum size, with no backups (set maxSizeRollBackups to 0). Note that when the file reaches its max size, it truncates (basically deletes all existing logging and starts over):
FileAppender 类没有 MaxFileSize/MaximumFileSize 属性。如果您使用 RollingFileAppender,您只会得到这些。这是一个示例,它将您的文件限制为固定的最大大小,没有备份(将 maxSizeRollBackups 设置为 0)。请注意,当文件达到其最大大小时,它会截断(基本上删除所有现有日志记录并重新开始):
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="log.txt" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="0" />
<maximumFileSize value="10MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>
</appender>
回答by Mitch Wheat
The LogFileAppender does not support limiting the output file size (at least in the references I can find). To limit the file size, use a RollingFileAppender and roll on Size and set the file size limit.
LogFileAppender 不支持限制输出文件大小(至少在我能找到的参考文献中)。要限制文件大小,请使用 RollingFileAppender 并滚动 Size 并设置文件大小限制。
To limit the number of roll over files use the MaxSizeRollBackupsattribute
要限制翻转文件的数量,请使用MaxSizeRollBackups属性

