有没有办法以编程方式刷新 log4net 中的缓冲区

发布于 2024-08-17 16:30:54 字数 357 浏览 5 评论 0原文

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

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

这可能吗?

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?

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).

Is that possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

雾里花 2024-08-24 16:30:54

假设您正在使用开箱即用的 log4net,您可以深入研究并使用 log4net。像这样刷新附加程序:

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 在下面的评论中指出的那样,如果您想刷新整个存储库中的所有附加程序,您可以稍微简化代码,如下所示:

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

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

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();
        }
    }
}
枯叶蝶 2024-08-24 16:30:54

今天提供了更简单的选项:

LogManager.Flush();

刷新默认存储库中所有配置的附加程序中缓冲的日志记录事件。
https://logging.apache.org/log4net/release/sdk/html /M_log4net_LogManager_Flush.htm

强烈建议添加超时,例如

LogManager.Flush(3000);

Today simpler option is available:

LogManager.Flush();

Flushes logging events buffered in all configured appenders in the default repository.
https://logging.apache.org/log4net/release/sdk/html/M_log4net_LogManager_Flush.htm

It is highly recommended to add a timeout, like

LogManager.Flush(3000);
挽袖吟 2024-08-24 16:30:54

即使使用 Flush 或设置 ImmediateFlush,更改也不会立即反映在日志文件中。为了触发 FileSystemWatcher 事件,您可以这样做

if (appender.ImmediateFlush)
  {
    using (FileStream fs = new FileStream(appender.File, 
                           FileMode.Open, FileAccess.Read, ileShare.ReadWrite))
    { }
  }

Even if Flush is used or ImmediateFlush is set, changes are not reflected immediately in the log file. In order for the FileSystemWatcher events to fire, you can do this

if (appender.ImmediateFlush)
  {
    using (FileStream fs = new FileStream(appender.File, 
                           FileMode.Open, FileAccess.Read, ileShare.ReadWrite))
    { }
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文