Difference between Set method and SetScoped method in MappedDiagnosticsLogicalContext Class

Viewed 328

In NLog there are two methods Set and SetScoped in MappedDiagnosticsLogicalContext Class

Does SetScoped method sets the scope to - per request and if yes - what would be the scope for Set method?

1 Answers

The Set and the SetScoped are doing basically the same thing, except that the SetScoped returns a IDisposable which you could dispose for un-setting the value.

e.g. With Set

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

vs SetScoped

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

The scope of the MappedDiagnosticsLogicalContext (MDLC) is the current thread and childs threads. So without remove/dispose it will be available on those threads. See also: MDLC docs

Related