What's the difference between log4net.ThreadLogicalContext and log4net.ThreadContext

前端 未结 2 1717
陌清茗
陌清茗 2021-01-22 23:28

I don\'t understand the explanation in offical document:

Logical threads can jump from one managed thread to another.

What\'s the different between Thre

2条回答
  •  醉话见心
    2021-01-23 00:10

    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!

提交回复
热议问题