与AdoNetAppender冲洗log4net的缓冲 [英] Flush Log4Net buffer with AdoNetAppender
问题描述
我使用log4net的有AdoNetAppender,我喜欢冲在特定的时间缓冲,或时间段。
可能是我没错,log4net的不提供此功能并为我创造我自己的Appender,但我不知道,也可以是我混淆如何实现这一点。
谁能帮我?
有关平齐,我想用下面的code,但它没有时间或超时功能。
公共无效FlushBuffers()
{
ILoggerRepository代表= LogManager.GetRepository();
的foreach(在rep.GetAppenders IAppender追加程序())
{
VAR缓冲=附加器作为BufferingAppenderSkeleton;
如果(缓冲!= NULL)
{
buffered.Flush();
}
}
}
修改
log4net的是Asp.Net网站实现的,我需要刷新该扔的网站。
也许我可以创建线程这在每2个小时执行code块,并刷新缓冲区。
公共类FlushingAdoNetAppender:AdoNetAppender
{
私人定时器flushTimer;
私人时间跨度flushInterval = TimeSpan.FromMinutes(5); 公共FlushingAdoNetAppender()
{
//启用调试的目的
// LogLog.InternalDebugging = TRUE;
} 公开时间跨度FlushInterval
{
/ * 在此之后,缓冲器会被刷新的时间间隔。默认设置为5分钟 示例配置: <追加程序名称=DatabaseAppenderTYPE =Your.Namespace.FlushingAdoNetAppender>
< flushInterval值=00:30:00/>
< /附加器> * / {返回flushInterval; }
集合{flushInterval =价值; }
} 公共覆盖无效ActivateOptions()
{
flushTimer =新的Timer(flushInterval.TotalMilliseconds); LogLog.Debug(的GetType(),刷新计时器间隔为+ TimeSpan.FromMilliseconds(flushTimer.Interval)); flushTimer.Enabled = TRUE;
flushTimer.Elapsed + = FlushLog;
flushTimer.Start(); base.ActivateOptions();
} 保护覆盖无效的OnClose()
{
//这是通过log4net的重新加载配置时称为
flushTimer.Stop();
flushTimer.Dispose();
base.OnClose(); //调用flush()
} 私人无效FlushLog(对象发件人,ElapsedEventArgs E)
{
LogLog.Debug(的GetType(),法拉盛日志);
同花顺();
}
}
I'M using Log4Net with AdoNetAppender, and I like to flush buffer in particular time, or time period.
May be I'M not wrong, Log4Net do not provide this functionality and for that I have to create my own Appender, but I have no idea or may be I am confuse how to implement this.
Can anyone help me with this?
For flush I am suppose to use following code but it do not have time or timeout functionality.
public void FlushBuffers()
{
ILoggerRepository rep = LogManager.GetRepository();
foreach (IAppender appender in rep.GetAppenders())
{
var buffered = appender as BufferingAppenderSkeleton;
if (buffered != null)
{
buffered.Flush();
}
}
}
EDIT
Log4Net is implemented in Asp.Net website, and I need to flush this throw the site.
May be I can create Thread which execute code block in every 2 hours and flush the buffer.
public class FlushingAdoNetAppender : AdoNetAppender
{
private Timer flushTimer;
private TimeSpan flushInterval = TimeSpan.FromMinutes(5);
public FlushingAdoNetAppender()
{
// Enable for debugging purposes
// LogLog.InternalDebugging = true;
}
public TimeSpan FlushInterval
{
/*
The interval after which the buffer will be flushed. Defaults to 5 minutes
Example config:
<appender name="DatabaseAppender" type="Your.Namespace.FlushingAdoNetAppender">
<flushInterval value="00:30:00" />
</appender>
*/
get { return flushInterval; }
set { flushInterval = value; }
}
public override void ActivateOptions()
{
flushTimer = new Timer(flushInterval.TotalMilliseconds);
LogLog.Debug(GetType(), "Flush timer interval is " + TimeSpan.FromMilliseconds(flushTimer.Interval));
flushTimer.Enabled = true;
flushTimer.Elapsed += FlushLog;
flushTimer.Start();
base.ActivateOptions();
}
protected override void OnClose()
{
// This is called by log4net when reloading the config
flushTimer.Stop();
flushTimer.Dispose();
base.OnClose(); // calls Flush()
}
private void FlushLog(object sender, ElapsedEventArgs e)
{
LogLog.Debug(GetType(), "Flushing logs");
Flush();
}
}
这篇关于与AdoNetAppender冲洗log4net的缓冲的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!