简体   繁体   English

使用AdoNetAppender刷新Log4Net缓冲区

[英]Flush Log4Net buffer with AdoNetAppender

I'M using Log4Net with AdoNetAppender, and I like to flush buffer in particular time, or time period. 我将Log4Net与AdoNetAppender一起使用,并且我想在特定时间或特定时间段内刷新缓冲区。

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. 可能是我没看错,Log4Net不提供此功能,为此我必须创建自己的Appender,但是我不知道,或者可能是我对如何实现此方法感到困惑。

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. Log4Net是在Asp.Net网站中实现的,因此需要刷新该网站。

May be I can create Thread which execute code block in every 2 hours and flush the buffer. 可能是我可以创建每2小时执行一次代码块并刷新缓冲区的Thread。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM