log4net.ThreadLogicalContext 和 log4net.ThreadContext 有什么区别

发布于 2024-10-09 01:44:23 字数 142 浏览 0 评论 0原文

我不明白官方文档中的解释:

逻辑线程可以从一个托管线程跳转到另一个托管线程。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

时光瘦了 2024-10-16 01:44:23

我应该回去并将其添加到我自己的问题中(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!

哎呦我呸! 2024-10-16 01:44:23

通过我自己的使用,我看到了在处理多线程逻辑(asyncawait)时使用 ThreadLogicalContext 的好处。

例如,如果您使用 ThreadContext 在原始调用线程上设置该属性,则该属性也可用于在同一线程上运行的任何其他任务。

// caller thread (thread #1)
log4net.ThreadContext.Properties["MyProp"] = "123"; // now set on thread #1
log("start");
await Task.WhenAll(
  MyAsync(1), // `Issue` if task run on thread #1, it will have "MyProp"
  MyAsync(2)  // `Issue` if task run on thread #1, it will have "MyProp"
);
log("end"); // `Issue` only by random chance will you run on thread #1 again

就好像您使用 ThreadLogicalContext 一样,它保留在调用上下文中。

// caller thread (thread #1)
log4net.LogicalThreadContext.Properties["MyProp"] = "123"; // now set on calling context
log("start");
await Task.WhenAll(
    MyAsync(1), // if task run on thread #1, there is no "MyProp"
    MyAsync(2)  // if task run on thread #1, there is no "MyProp"
);
log("end"); // if task run on thread #1, there is no "MyProp"

使用 await ,您永远无法保证返回到启动时的同一个线程,并且调用上下文将发生变化,因此您必须再次设置该属性。

...
log4net.LogicalThreadContext.Properties["MyProp"] = "123";
log("end");

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.

// caller thread (thread #1)
log4net.ThreadContext.Properties["MyProp"] = "123"; // now set on thread #1
log("start");
await Task.WhenAll(
  MyAsync(1), // `Issue` if task run on thread #1, it will have "MyProp"
  MyAsync(2)  // `Issue` if task run on thread #1, it will have "MyProp"
);
log("end"); // `Issue` only by random chance will you run on thread #1 again

Where as if you use ThreadLogicalContext, it stays on the calling context.

// caller thread (thread #1)
log4net.LogicalThreadContext.Properties["MyProp"] = "123"; // now set on calling context
log("start");
await Task.WhenAll(
    MyAsync(1), // if task run on thread #1, there is no "MyProp"
    MyAsync(2)  // if task run on thread #1, there is no "MyProp"
);
log("end"); // if task run on thread #1, there is no "MyProp"

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.

...
log4net.LogicalThreadContext.Properties["MyProp"] = "123";
log("end");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文