log4net RollingFileAppender 不“滚动”当以编程方式配置时
我正在尝试以编程方式在 C# 中创建滚动文件附加程序。我正在使用 Visual Studios 2008。我正在使用 log4net 版本 1.2.0.30714。
我的主要问题是我的滚动文件附加器的行为就像文件附加器。日志文件不会根据我给它的任何大小或日期标准滚动。以下是我的配置,我将不胜感激任何见解或建议。下面的代码是我尝试过的一些想法。
string path = "Logs\";
string filename = "test.log";
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
log4net.Layout.PatternLayout patternLayout = new log4net.Layout.PatternLayout("[%d] %l %n - %m %n%n");
patternLayout.ActivateOptions();
RollingFileAppender appender = new RollingFileAppender();
appender.Threshold = Level.ALL;
appender.StaticLogFileName = false;
appender.CountDirection = 0; // Makes the logs count 1, 2, 3
appender.RollingStyle = RollingFileAppender.RollingMode.Size;
appender.MaximumFileSize = "1KB";
appender.MaxFileSize = 1 * 1024; // 1 KB
appender.MaxSizeRollBackups = 5;
appender.DatePattern = "yy.MM.dd.hh.mm.ss";
appender.Layout = patternLayout;
appender.Name = "QTracImportHelper";
appender.ImmediateFlush = true;
appender.File = filename;
appender.Writer = new StreamWriter(path + filename, true);
appender.ActivateOptions();
Hierarchy hierarchy = (Hierarchy)LogManager.GetLoggerRepository();
hierarchy.Root.AddAppender(appender);
hierarchy.Configured = true;
我注意到删除 StreamWriter 会导致根本不会创建任何文件,但也不例外。删除文件名会导致 log4net 内出现异常。我尝试同时使用 MaxFileSize 和 MaximumFileSize,但没有成功,也尝试将滚动栅格更改为“日期”和“复合”。此外,如果写入器流格式错误,它将创建与不包含 File 属性相同的异常({“Value 不能为 null。\r\nParameter name: fileName”}{System.ArgumentNullException})。
I am attempting to programmatically create a rolling file appender in C#. I'm using Visual Studios 2008. I am using log4net version 1.2.0.30714.
My main issue is that my rolling file appender is acting like a file appender. The log file will not roll based upon any size or date criteria I give it. Below is my configuration and I would appreciate any insight or suggestions. Below the code is some of the ideas I have tried.
string path = "Logs\";
string filename = "test.log";
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
log4net.Layout.PatternLayout patternLayout = new log4net.Layout.PatternLayout("[%d] %l %n - %m %n%n");
patternLayout.ActivateOptions();
RollingFileAppender appender = new RollingFileAppender();
appender.Threshold = Level.ALL;
appender.StaticLogFileName = false;
appender.CountDirection = 0; // Makes the logs count 1, 2, 3
appender.RollingStyle = RollingFileAppender.RollingMode.Size;
appender.MaximumFileSize = "1KB";
appender.MaxFileSize = 1 * 1024; // 1 KB
appender.MaxSizeRollBackups = 5;
appender.DatePattern = "yy.MM.dd.hh.mm.ss";
appender.Layout = patternLayout;
appender.Name = "QTracImportHelper";
appender.ImmediateFlush = true;
appender.File = filename;
appender.Writer = new StreamWriter(path + filename, true);
appender.ActivateOptions();
Hierarchy hierarchy = (Hierarchy)LogManager.GetLoggerRepository();
hierarchy.Root.AddAppender(appender);
hierarchy.Configured = true;
I've noticed that removing the StreamWriter leads to no file being created at all but no exceptions. Removing the file name causes an exception from within log4net. I have tried using both MaxFileSize and MaximumFileSize to no avail as well as changing the rolling stile to Date and Composite. Additionally if the writer stream is malformed it will create the same exception as not including the File property ({"Value cannot be null.\r\nParameter name: fileName"}{System.ArgumentNullException}).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为这可能是由于您在 ActivateOptions() 调用之前直接提供了“Writer”参数。编写器应该由 RollingLogFileAppender 打开,而不是从外部打开。它只是因为继承了 TextWriterAppender 类而暴露。
I think this may be due to you supplying the "Writer" parameter directly before the ActivateOptions() call. The writer should be opened by the RollingLogFileAppender not externally. It's only exposed because of the inheritance of the class from TextWriterAppender.