I am trying to move into .net core an existing .net application that is using CallContext.LogicalGet/SetData.
When a web request hits the application I save a CorrelationId in the CallContext and whenever I need to log something later down the track I can easily collect it from the CallContext, without the need to transfer it everywhere.
As CallContext is no longer supported in .net core since it is part of System.Messaging.Remoting what options are there?
One version I have seen is that the AsyncLocal could be used (How do the semantics of AsyncLocal differ from the logical call context?) but it looks as if I would have to transmit this variable all over which beats the purpose, it is not as convenient.
Had this problem when we switched a library from .Net Framework to .Net Standard and had to replace
System.Runtime.Remoting.Messaging
CallContext.LogicalGetData
andCallContext.LogicalSetData
.I followed this guide to replace the methods:
http://www.cazzulino.com/callcontext-netstandard-netcore.html
You can use a dictionary of AsyncLocal to simulate exactly the API and behavior of the original CallContext. See http://www.cazzulino.com/callcontext-netstandard-netcore.html for a complete implementation example.
I guess you could use ThreadStatic since what I heard CallContext is/was "just" an abstraction over ThreadStatic with automatic transfer of state between threads in the call stack.
Also, you could/should use AsyncLocal/ThreadLocal instead. AsyncLocal seem to be pretty much what CallContext was.