0

Is there a way to read the value of an AsyncLocal value as another thread would see it?

Assume I have a thread executing and there I set a value. Now from another thread, with means like reflection or Microsoft.Diagnostics.Runtime, I would like to read the value of this AsyncLocal and get the value that would be returned if I would access it from the first thread. I want to observe what value is there in another async flow from the outside.

I know how AsyncLocal works and that it is not a value "fixed" to a thread. But I would like to know the value that a particular thread would get when reading the AsyncLocal.

Important

In the past I had a value in the LogicalCallContext on the ExecutionContext of a thread and was able to read it through reflection. So now I look for an alternative that works with AsyncLocal. The actual application of this was a diagnostics functionality.

Update

Looking at the code of AsyncLocale here made me thing there might be a way to retrieve this informaiton somehow. But when I tried I only saw that I would be able to read some AsyncLocales but not the one I was actually looking for. So I would be eager to know where this information is living in the CLR. Where is the data of the AsyncLocale living in the CLR?

Sjoerd222888
  • 3,228
  • 3
  • 31
  • 64
  • 2
    `AsyncLocal` flows with the async flow, and async flow can flow across many threads - which is why `AsyncLocal` was invented in the first place. So it doesn't make sense to ask for the AsyncLocal *of a thread*. Are you actually talking about *threads* (because [async is not about threads](https://stackoverflow.com/q/17661428/11683)) or *tasks in flight*? – GSerg Aug 10 '21 at 16:10
  • Yes I know. I want to read data from the current async flow that a given thread is executing. – Sjoerd222888 Aug 10 '21 at 17:27

0 Answers0