NLOG:关闭不会使目标处置

发布于 2025-02-03 05:38:38 字数 1463 浏览 4 评论 0原文

我添加了一个新的nlog目标,需要创建一个附加的logFactory。新的目标看起来像这样:

[Target("NLogReplayCache")]
public sealed class NLogReplayCache : Target
{
    private readonly TransactionList<NLog.LogEventInfo> logs = new();

    /// <summary>
    /// Holds a list of the current log messages.
    /// </summary>
    public IReadOnlyTransactionList<NLog.LogEventInfo> Logs => logs;


    public NLogReplayCache()
    {
    }

    protected override void Dispose(bool disposing)
    {
        base.Dispose(disposing);
    }

    protected override void Write(LogEventInfo logEvent)
    {
        logs.BeginUpdate().Add(logEvent).Dispose();
    }

    public void Clear()
    {
        logs.BeginUpdate().Clear().Dispose();
    }
}

nlogrocontask.config看起来像这样:

    <target
              name="cache"
              xsi:type="AsyncWrapper"
              timeToSleepBetweenBatches="0"
              queueLimit="1000"
              overflowAction="Grow">
                <target xsi:type="NLogReplayCache"/>
   </target>

创建了新的logfactory:

  NLog.LogFactory loggerFactory = new NLog.LogFactory();
  loggerFactory.LoadConfiguration("NLogReconTask.config");

完成了我需要将其关闭并将其放置后的记录器后:

    loggerFactory?.Shutdown();
    loggerFactory?.Dispose();

我看不到的是distose未调用。这是预期的吗?

I have added a new NLog target and need to create an additional LogFactory. The new target looks like this:

[Target("NLogReplayCache")]
public sealed class NLogReplayCache : Target
{
    private readonly TransactionList<NLog.LogEventInfo> logs = new();

    /// <summary>
    /// Holds a list of the current log messages.
    /// </summary>
    public IReadOnlyTransactionList<NLog.LogEventInfo> Logs => logs;


    public NLogReplayCache()
    {
    }

    protected override void Dispose(bool disposing)
    {
        base.Dispose(disposing);
    }

    protected override void Write(LogEventInfo logEvent)
    {
        logs.BeginUpdate().Add(logEvent).Dispose();
    }

    public void Clear()
    {
        logs.BeginUpdate().Clear().Dispose();
    }
}

and the NLogReconTask.config looks likew this:

    <target
              name="cache"
              xsi:type="AsyncWrapper"
              timeToSleepBetweenBatches="0"
              queueLimit="1000"
              overflowAction="Grow">
                <target xsi:type="NLogReplayCache"/>
   </target>

and the new LogFactory is created like this:

  NLog.LogFactory loggerFactory = new NLog.LogFactory();
  loggerFactory.LoadConfiguration("NLogReconTask.config");

after I'm finished with the logger I need to shut it down and dispose it:

    loggerFactory?.Shutdown();
    loggerFactory?.Dispose();

What I cannot see is that the Dispose from the target is not called. Is this expected?

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

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

发布评论

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

评论(1

-残月青衣踏尘吟 2025-02-10 05:38:38

正如@Rolfkristensen指出的那样,使用initializetarget()coleteTarget()是要使用的方法。尽管如此,这再次是何时使用iDisposable的好奇心的一个示例。

As @RolfKristensen pointed out, using InitializeTarget() and CloseTarget() are the methods to be used. Nevertheless this is again an example of the curiosities around IDisposable of when to use it and how.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文