NLOG:关闭不会使目标处置
我添加了一个新的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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如@Rolfkristensen指出的那样,使用
initializetarget()
和coleteTarget()
是要使用的方法。尽管如此,这再次是何时使用iDisposable
的好奇心的一个示例。As @RolfKristensen pointed out, using
InitializeTarget()
andCloseTarget()
are the methods to be used. Nevertheless this is again an example of the curiosities aroundIDisposable
of when to use it and how.