I want to write a logging http module that stores a list of log events for a single request in thread local storage while the request executes. On End_Request I want to write all the events back to persistent storage.
Question is, will one request match to one thread? I.e. can I assume from anywhere in my code that I can add items to the IEnumerable and they will properly be all together at the end of the request.
No. ASP.NET can potentially switch threads while processing a request. This is known as thread-agility.
There are only certain points where it can/will do this. I can't remember what they are off the top of my head - searching for a reference now...
But the short answer is NO: you can't rely on the same thread-local storage being accessible for the entire duration of the request.
You might be better off using Context.Items rather than thread storage - that's per request. You don't need to worry about what the server is doing with its threads that way.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With