正确的方法来关闭一个记录器实例中log4net的记录器、方法来、实例、正确

由网友(僵尸吃掉了你的老子)分享简介:我要的每一个实例创建一个新的记录器和武官一个缓冲的appender和flie附加器给它一个类。 一切正在做的运行时,没有任何信息是从配置文件中挑选出来的。的 I have a class to whose every instance i create a new logger and attache a buff...

我要的每一个实例创建一个新的记录器和武官一个缓冲的appender和flie附加器给它一个类。 一切正在做的运行时,没有任何信息是从配置文件中挑选出来的。的

I have a class to whose every instance i create a new logger and attache a buffer appender and a flie appender to it. Everything is being done runtime and no information is picked from the config file.

现在释放资源类的自定义Dispose方法,我需要关闭的具体记录,并释放所有的附加资源,以避免任何内存泄漏。

Now to release resources at the class's custom dispose method i need to shutdown that specific logger and release all of its attached resources so as to avoid any memory leak.

目前,我一直在做的是ATLEAST刷新文件附加器和写入所有日志信息,但是,无论是发布在特定的日志文件中的锁,也没有发布任何资源。

At the moment what i have been doing is atleast flush the file appender and write all logging information but that neither releases the lock on that specific logging file nor does it release any of its resources.

什么是关闭的记录,而不是关停等有效记录器正在处理

What is the proper way of shutting down the logger while not shutting down other active loggers that are in process

log4net.ILog log = log4net.LogManager.GetLogger(loggerName);

foreach (IAppender iapp in log.Logger.Repository.GetAppenders())
{
    BufferingAppenderSkeleton buffered = iapp as BufferingAppenderSkeleton;
    if (buffered is BufferingForwardingAppender)
    {
        ((BufferingForwardingAppender)buffered).Flush();
    }
}

log.Logger.Repository.Shutdown();

我希望我已经做了我的问题不够清楚:)

I hope i have made my question clear enough :)

推荐答案

在这种情况下,因为你不分享任何追加程序,你应该能够使用IAppender.Close()方法在所有连接到您的记录仪的附加目的地(这也将导致他们都被刷新)。

In this instance, as you are not sharing any appenders, you should be able to use the IAppender.Close() method on all the appenders attached to your logger (this will also cause them all to be flushed).

您应该投记录器IAppenderAttachable,并获得追加程序形成有;这将允许你以确保你只调用close()在你的嵌套追加程序的顶层。这应当引起它们刷新和关闭自己的孩子以正确的顺序。

You should cast the logger to IAppenderAttachable and get the appenders form there; this will allow you to make sure you only call Close() on the top level of your nested appenders. This should cause them to flush and close down their own children in the correct order.

的http://logging.apache.org/log4net/release/sdk/log4net.Appender.IAppender.Close.html

这将是非常危险的,如果你使用的是标准的log4net的设置与配置!

This will be very dangerous if you are using a standard log4net setup with a configuration!

阅读全文

相关推荐

最新文章