有没有一种方法可以以编程方式刷新log4net中的缓冲区 [英] Is there a way to programmably flush the buffer in log4net

查看:84
本文介绍了有没有一种方法可以以编程方式刷新log4net中的缓冲区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将log4net与AdoNetAppender一起使用.看来AdoNetAppender具有 Flush方法.无论如何,我可以从代码中调用它吗?

I'm using log4net with AdoNetAppender. It's seems that the AdoNetAppender has a Flush method. Is there anyway I can call that from my code?

我正在尝试创建一个管理页面来查看数据库日志中的所有条目,并且我想使用bufferSize = 100(或更大)设置log4net,然后希望管理员能够单击一个按钮在管理页面上,以强制log4net将缓冲的日志条目写入数据库(不关闭log4net).

I'm trying to create an admin page to view all the entries in the database log, and I will like to setup log4net with bufferSize=100 (or more), then I want the administrator to be able to click an button on the admin page to force log4net to write the buffered log entries to the database (without shutting down log4net).

有可能吗?

推荐答案

假设您使用的是开箱即​​用的log4net,则可以深入了解&像这样刷新appender:

Assuming you're using log4net out of the box, you can dig your way down & flush the appender like this:

public void FlushBuffers()
{
    ILog log = LogManager.GetLogger("whatever");
    var logger = log.Logger as Logger;
    if (logger != null)
    {
        foreach (IAppender appender in logger.Appenders)
        {
            var buffered = appender as BufferingAppenderSkeleton;
            if (buffered != null)
            {
                buffered.Flush();
            }
        }
    }
}


编辑:我在上面的假设下编写了上面的代码,即您想刷新特定ILog的附加程序(现在我重新阅读该问题可能是一个错误的假设),但我是Stefan在下面的注释中指出,如果要按如下所示刷新整个存储库中的所有附加程序,可以稍微简化代码:


Edit: I wrote the above under the assumption that you wanted to flush the appenders for a specific ILog (probably a bad assumption now that I re-read the question), but as Stefan points out in a comment below, you can simplify the code a little if you want to flush all appenders across the whole repository as follows:

public void FlushBuffers()
{
    ILoggerRepository rep = LogManager.GetRepository();
    foreach (IAppender appender in rep.GetAppenders())
    {
        var buffered = appender as BufferingAppenderSkeleton;
        if (buffered != null)
        {
            buffered.Flush();
        }
    }
}

这篇关于有没有一种方法可以以编程方式刷新log4net中的缓冲区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆