MappedDiagnosticsLogicalContext 中 Set 方法和 SetScoped 方法的区别 Class

Difference between Set method and SetScoped method in MappedDiagnosticsLogicalContext Class

在NLog中有两种方法SetSetScopedMappedDiagnosticsLogicalContextClass

SetScoped 方法是否将范围设置为 - 每个请求,如果是 - Set 方法的范围是什么?

SetSetScoped 基本上做同样的事情,除了 SetScoped returns 一个 IDisposable 你可以为 un-setting 值。

例如随着 Set

MappedDiagnosticsLogicalContext.Set("key1", "value1");
DoSomething();
MappedDiagnosticsLogicalContext.Remove("key1");

对比SetScoped

using(MappedDiagnosticsLogicalContext.SetScoped("key1", "value1"))
{
    DoSomething();
}

MappedDiagnosticsLogicalContext (MDLC) 的范围是当前线程和子线程。所以没有 remove/dispose 它将在那些线程上可用。另见:MDLC docs