log4net.ThreadLogicalContext 和 log4net.ThreadContext 有什么区别
我不明白官方文档中的解释:
逻辑线程可以从一个托管线程跳转到另一个托管线程。
ThreadContext 和 ThreadLogicalContext 有什么区别? 有人可以详细说明一下吗?
谢谢。
I don't understand the explanation in offical document:
Logical threads can jump from one managed thread to another.
What's the different between ThreadContext and ThreadLogicalContext?
Can someone elaborate on it?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我应该回去并将其添加到我自己的问题中(Stefan Egli 在上面链接)......
据我所知,两者之间几乎没有实际差异。
ThreadContext 将信息存储在使用 Thread.SetData 存储的字典中。
ThreadLogicalContext 将其信息存储在使用 CallContext 存储的字典中。
CallContext中存储的信息几乎相同
可访问性作为使用 Thread.SetData 存储的信息。也就是说,首先存储该信息的线程可以访问该信息。
现在,如果 ThreadLogicalContext 使用 CallContext.LogicalSetData(或者如果使用 CallContext.SetData 存储的字典实现了标记接口 IThreadAffinative),那么将会有很大的差异。在这种情况下,可以在同一线程中访问存储的任何信息 (LogicalSetData),并将其传递给子线程。此外(与逻辑线程一起流动),
信息可以跨远程调用和跨应用程序域流动(如果数据是可序列化的)。
我本想添加一些链接,但我是在 iPhone 上工作,所以有点尴尬。 Stefan Egli 上面发布的链接中有一些很好的链接。
另外,请参阅 Jeffrey Richter 9 月份的博客,获取有关 CallContext.LogicalSetData 的文章。我使用他的测试程序作为比较 CallContext.SetData 与 CallContext.LogicalSetData 与 Thread.SetData 与 [ThreadStatic] 的基础。上次检查是最后一次
他发布的东西。
当我可以轻松访问计算机时,我会尝试回来发布更多链接和/或一些示例代码。
祝你好运!
I should go back and add this to my own question (that Stefan Egli linked above) ...
From what I can tell, there is very little practical difference between the two.
ThreadContext stores information in a Dictionary that is stored using Thread.SetData.
ThreadLogicalContext stores its information in a Dictionary that is stored using the CallContext.
Information stored in the CallContext has almost the same
accessibility as information stored using Thread.SetData. That is, the information is accessibli to the thread that stored the information in the first place.
Now, IF the ThreadLogicalContext used CallContext.LogicalSetData (or if the Dictionary stored using CallContext.SetData implemented the marker interface, IThreadAffinative) then there WOULD be BIG difference. In that case, any information stored (LogicalSetData) could be accessed within the same thread AND is passed to child threads. In addition (flows with the logical thread), the
information can flow across remoting calls and across AppDomains (if the data is Serializable).
I would have put in some links, but am working from iPhone so is a little awkward. There are some good links in the link that Stefan Egli posted above.
Also, look at Jeffrey Richter's blog from September for an article on CallContext.LogicalSetData. I used his test program as a basis for comparing CallContext.SetData vs CallContext.LogicalSetData vs Thread.SetData vs [ThreadStatic]. Last time I checked, it was the last
thing he posted.
Will try to come back and post more links and/or some sample code when I have easy access to computer.
Good luck!
通过我自己的使用,我看到了在处理多线程逻辑(
async
、await
)时使用 ThreadLogicalContext 的好处。例如,如果您使用 ThreadContext 在原始调用线程上设置该属性,则该属性也可用于在同一线程上运行的任何其他任务。
就好像您使用 ThreadLogicalContext 一样,它保留在调用上下文中。
使用
await
,您永远无法保证返回到启动时的同一个线程,并且调用上下文将发生变化,因此您必须再次设置该属性。From using this myself, I see the benefit of using the ThreadLogicalContext when working with multi threaded logic (
async
,await
).For example, if you set the property on your original calling thread using
ThreadContext
, it is also available to any other tasks that get to run on the same thread.Where as if you use
ThreadLogicalContext
, it stays on the calling context.With
await
you are never guaranteed you come back to the same thread as when you started and the calling context will have changed, so you will have to set the property again.