I have a WCF service and it logs each call to the database. Later on, if an exception occurs, it is also logged to a separate database.
I wanted a way to tie both of these logs together so we can see what might have caused the exception. To do this, I wanted some kind of unique ID that I could get for each call.
Since the whole thing is executing on a single thread, I could for example, set the thread name to a GUID eg. System.Threading.Thread.CurrentThread.Name = Guid.NewGuid().ToString();
but this is a bit hacky.
Searching around the net, I discovered System.Threading.Thread.CurrentContext.SetProperty()
but I am wondering exactly what that context is. Is it designed to store properties for the duration of a thread? Is it unique per thread?
If I have 5 simultaneous WCF calls I don't want there to be any conflicts between what's going in the context if it's not 'per call' so to speak.
Could someone clarify?
ThreadLocal
toAsyncLocal
, this allows your storage to flow across a async/await boundary where you could end up jumping threads. – Merengue