How to multi-thread "Cross-tier" a singleton with a correlation id without locking?

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:

  1. Process 1 has fired by user 1
  2. Correlation ID gets set to {1}
  3. DLL A accesses state context and gets correlation ID {1}
  4. Before process 1 is completed, Process 2 has fired by user 2
  5. Correlation ID gets set to {2}
  6. DLL B from 1st process access state context with correlation id {2} when it should be {1}

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

Answers


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.


Need Your Help

R: map.match on world database returns "Error in tolower(patterns) : object 'patterns' not found"

r maps

I'm attempting to create a spatial map using the maps package in R. However, when I try to use match.map() using the world database, I receive the following error:

Implementing Content Provider: compound primary key

android android-contentprovider

I'm implementing my own Content Provider because I'm gonna synchronize my database with a server. My data is stored in a SQLiteDatabase, and some of my tables have a compound primary key (2 columns...