0

I am using NLog 4.5.10 with C# 4.7.1 inside a REST server. There I am using the method MappedDiagnosticsLogicalContext.Set(item, value) to add some parameters of the request to my log message.

The question now: Do I have to clean them up manually or are these dictionaries garbage collected, too?

As far as I know, the dictionaries are bound to the current Thread (by using the ThreadId?). What happens, when I have many requests and the ThreadId starts to count at zero again? May it happen, that the values of the last thread with the same Id are still there, or are those objects destroyed, when the corresponding thread is destroyed?

Best regards, Daniel

Daniel P.
  • 43
  • 4

1 Answers1

0

The async context will be "destroyed" when the thread-object is garbage collected. But if the thread lives in a threadpool, then it might live "forever".

Maybe you can make use of SetScoped:

using (MappedDiagnosticsLogicalContext.SetScoped("Property", "PropertyValue")) {
    // "Property" item is present in current context
}

See also https://github.com/NLog/NLog/wiki/MDLC-Layout-Renderer#scoped-item

Rolf Kristensen
  • 17,785
  • 1
  • 51
  • 70