log4net 是否提供最大速率功能,即允许将日志写入文件的最大速度?
Does log4net provides Max Rate feature i.e. maximum speed allowed to write logs to the file?
我有兴趣限制每秒的日志行数。我只需要限制每秒的日志行数,因为与可能生成可记录事件的子系统相比,记录子系统通常很慢。如果没有某种保护,攻击者可能会淹没审核服务器,或者让设备忙于发送重复的审核消息以致核心功能停止工作。
正如我在评论中提到的,您可以编写自己的 ForwardingAppender。这是控制台应用程序中的示例:
Program.cs
using log4net;
using log4net.Config;
using log4net.Repository;
using System;
using System.IO;
using System.Reflection;
using System.Threading;
namespace MaxRateExample
{
public static class Program
{
private static readonly ILog log = LogManager.GetLogger(typeof(Program));
static void Main(string[] args)
{
ILoggerRepository logRepository = LogManager.GetRepository(Assembly.GetEntryAssembly());
XmlConfigurator.Configure(logRepository, new FileInfo("log4net.config"));
for (int i = 0; i < 1000; i++)
{
Console.WriteLine($"item-{i}");
log.Info($"item-{i}");
Thread.Sleep(10);
}
}
}
}
MaxRateForwardingAppender.cs
using log4net.Appender;
using log4net.Core;
using System;
using System.Collections.Generic;
namespace MaxRateExample
{
public class MaxRateForwardingAppender : ForwardingAppender
{
// Maximum 10 events per second. You can move this to config file.
private const int MaxRateSeconds = 1;
private const int MaxRateEvents = 10;
private List<DateTime> Dates { get; }
public MaxRateForwardingAppender()
{
Dates = new List<DateTime>();
}
protected override void Append(LoggingEvent loggingEvent)
{
if (LogEvent())
base.Append(loggingEvent);
}
protected override void Append(LoggingEvent[] loggingEvents)
{
if (LogEvent())
base.Append(loggingEvents);
}
private bool LogEvent()
{
Dates.RemoveAll(x => (DateTime.Now - x).Seconds > MaxRateSeconds);
if (Dates.Count <= MaxRateEvents)
{
Dates.Add(DateTime.Now);
return true;
}
return false;
}
}
}
log4net.config
<log4net>
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="C:\Logs\MyLog.log" />
<appendToFile value="true" />
<rollingStyle value="Date" />
<datePattern value="'.'yyyyMMdd'.log'" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %-5level %message%newline" />
</layout>
</appender>
<appender name="MaxRateForwardingAppender" type="MaxRateExample.MaxRateForwardingAppender, MaxRateExample">
<appender-ref ref="RollingFileAppender" />
</appender>
<root>
<appender-ref ref="MaxRateForwardingAppender" />
</root>
</log4net>
日志文件的示例输出:
2020-01-09 15:50:05,396 INFO item-0
2020-01-09 15:50:05,419 INFO item-1
2020-01-09 15:50:05,430 INFO item-2
2020-01-09 15:50:05,441 INFO item-3
2020-01-09 15:50:05,452 INFO item-4
2020-01-09 15:50:05,463 INFO item-5
2020-01-09 15:50:05,474 INFO item-6
2020-01-09 15:50:05,485 INFO item-7
2020-01-09 15:50:05,496 INFO item-8
2020-01-09 15:50:05,507 INFO item-9
2020-01-09 15:50:05,518 INFO item-10
2020-01-09 15:50:07,403 INFO item-183
2020-01-09 15:50:07,425 INFO item-185
2020-01-09 15:50:07,436 INFO item-186
2020-01-09 15:50:07,447 INFO item-187
2020-01-09 15:50:07,458 INFO item-188
2020-01-09 15:50:07,469 INFO item-189
2020-01-09 15:50:07,480 INFO item-190
2020-01-09 15:50:07,491 INFO item-191
2020-01-09 15:50:07,502 INFO item-192
2020-01-09 15:50:07,513 INFO item-193
2020-01-09 15:50:07,538 INFO item-194
2020-01-09 15:50:09,406 INFO item-366
2020-01-09 15:50:09,427 INFO item-368
2020-01-09 15:50:09,438 INFO item-369
2020-01-09 15:50:09,449 INFO item-370
2020-01-09 15:50:09,460 INFO item-371
2020-01-09 15:50:09,472 INFO item-372
我有兴趣限制每秒的日志行数。我只需要限制每秒的日志行数,因为与可能生成可记录事件的子系统相比,记录子系统通常很慢。如果没有某种保护,攻击者可能会淹没审核服务器,或者让设备忙于发送重复的审核消息以致核心功能停止工作。
正如我在评论中提到的,您可以编写自己的 ForwardingAppender。这是控制台应用程序中的示例:
Program.cs
using log4net;
using log4net.Config;
using log4net.Repository;
using System;
using System.IO;
using System.Reflection;
using System.Threading;
namespace MaxRateExample
{
public static class Program
{
private static readonly ILog log = LogManager.GetLogger(typeof(Program));
static void Main(string[] args)
{
ILoggerRepository logRepository = LogManager.GetRepository(Assembly.GetEntryAssembly());
XmlConfigurator.Configure(logRepository, new FileInfo("log4net.config"));
for (int i = 0; i < 1000; i++)
{
Console.WriteLine($"item-{i}");
log.Info($"item-{i}");
Thread.Sleep(10);
}
}
}
}
MaxRateForwardingAppender.cs
using log4net.Appender;
using log4net.Core;
using System;
using System.Collections.Generic;
namespace MaxRateExample
{
public class MaxRateForwardingAppender : ForwardingAppender
{
// Maximum 10 events per second. You can move this to config file.
private const int MaxRateSeconds = 1;
private const int MaxRateEvents = 10;
private List<DateTime> Dates { get; }
public MaxRateForwardingAppender()
{
Dates = new List<DateTime>();
}
protected override void Append(LoggingEvent loggingEvent)
{
if (LogEvent())
base.Append(loggingEvent);
}
protected override void Append(LoggingEvent[] loggingEvents)
{
if (LogEvent())
base.Append(loggingEvents);
}
private bool LogEvent()
{
Dates.RemoveAll(x => (DateTime.Now - x).Seconds > MaxRateSeconds);
if (Dates.Count <= MaxRateEvents)
{
Dates.Add(DateTime.Now);
return true;
}
return false;
}
}
}
log4net.config
<log4net>
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="C:\Logs\MyLog.log" />
<appendToFile value="true" />
<rollingStyle value="Date" />
<datePattern value="'.'yyyyMMdd'.log'" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %-5level %message%newline" />
</layout>
</appender>
<appender name="MaxRateForwardingAppender" type="MaxRateExample.MaxRateForwardingAppender, MaxRateExample">
<appender-ref ref="RollingFileAppender" />
</appender>
<root>
<appender-ref ref="MaxRateForwardingAppender" />
</root>
</log4net>
日志文件的示例输出:
2020-01-09 15:50:05,396 INFO item-0
2020-01-09 15:50:05,419 INFO item-1
2020-01-09 15:50:05,430 INFO item-2
2020-01-09 15:50:05,441 INFO item-3
2020-01-09 15:50:05,452 INFO item-4
2020-01-09 15:50:05,463 INFO item-5
2020-01-09 15:50:05,474 INFO item-6
2020-01-09 15:50:05,485 INFO item-7
2020-01-09 15:50:05,496 INFO item-8
2020-01-09 15:50:05,507 INFO item-9
2020-01-09 15:50:05,518 INFO item-10
2020-01-09 15:50:07,403 INFO item-183
2020-01-09 15:50:07,425 INFO item-185
2020-01-09 15:50:07,436 INFO item-186
2020-01-09 15:50:07,447 INFO item-187
2020-01-09 15:50:07,458 INFO item-188
2020-01-09 15:50:07,469 INFO item-189
2020-01-09 15:50:07,480 INFO item-190
2020-01-09 15:50:07,491 INFO item-191
2020-01-09 15:50:07,502 INFO item-192
2020-01-09 15:50:07,513 INFO item-193
2020-01-09 15:50:07,538 INFO item-194
2020-01-09 15:50:09,406 INFO item-366
2020-01-09 15:50:09,427 INFO item-368
2020-01-09 15:50:09,438 INFO item-369
2020-01-09 15:50:09,449 INFO item-370
2020-01-09 15:50:09,460 INFO item-371
2020-01-09 15:50:09,472 INFO item-372