So we are using a common state context singleton with a correlation ID for centralized Logging. The purpose is to track the id throughout our entire process and correlate the different tiers.
The state context is accessed by multiple dlls and multiple users.
The difficulty comes when multi threading comes into play:
How do we solve this issue?
We do not perceive locking to be our solution? Any other ideas?
Here is a diagram
(S)->[ CorrelationID {get;set} ]
^ ^ ^
U1 <--> | | | O
U2 <--> [DLLA] <--> [DLLB] <--> [DLLC] <--> | |
U3 <-->
{Web} <--> {Domain} <-> {Data Access} <--> {DB}
(<-- Process / Thread --> )
{} = Examples of possible DLLs
Each User's process should have 1 correlation ID
May be, ThreadLocal<T>? https://msdn.microsoft.com/en-us/library/dd642243%28v=vs.100%29.aspx
After much research we have found the solution.
We utilize the LogicalCallContext Class in the .Net Framework
So what the LogicalCalContext Class does is it utilizes the .Net Framework, it keeps the Key Values in the pool by preserving the callback, even if it jumps thread.
Learn how to Flow Data across threads...
Approach for tying all NLog logs back to the original request within WebAPI
The comment in the post references Serilog as well so you might want to take a look at that.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.