【发布时间】:2020-08-13 07:31:50
【问题描述】:
在 NLog 中有两个方法 Set 和 SetScoped 在 MappedDiagnosticsLogicalContext 类
SetScoped 方法是否将范围设置为 - 每个请求,如果是 - Set 方法的范围是什么?
【问题讨论】:
在 NLog 中有两个方法 Set 和 SetScoped 在 MappedDiagnosticsLogicalContext 类
SetScoped 方法是否将范围设置为 - 每个请求,如果是 - Set 方法的范围是什么?
【问题讨论】:
Set 和 SetScoped 做的事情基本相同,只是 SetScoped 返回一个 IDisposable,您可以将其用于取消设置值。
例如与Set
MappedDiagnosticsLogicalContext.Set("key1", "value1");
DoSomething();
MappedDiagnosticsLogicalContext.Remove("key1");
与SetScoped
using(MappedDiagnosticsLogicalContext.SetScoped("key1", "value1"))
{
DoSomething();
}
MappedDiagnosticsLogicalContext (MDLC) 的范围是当前线程和子线程。因此,如果没有删除/处置,它将在这些线程上可用。另见:MDLC docs
【讨论】: