log4net.ThreadContext和log4net.LogicalThreadContext之间的区别是什么? [英] What is the difference between log4net.ThreadContext and log4net.LogicalThreadContext?

查看:1210
本文介绍了log4net.ThreadContext和log4net.LogicalThreadContext之间的区别是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新于2014年11月18日 - 在浏览log4net的源代码库,我发现LogicalThreadContext的实施是在2011年11月修改了其存储使用CallContext.LogicalSetData它的属性(以及使用LogicalGetData让他们)。这很重要,因为这意味着LogicalThreadContext现在应该正常工作。存储在LogicalThreadContext应该是任何数据流的任何子线程或任务。与此相比,ThreadContext(和旧的实现LogicalThreadContext的),其中存储在所述上下文数据将保持本地到当前线程和NOT流到子线程/任务。

UPDATED on 11/18/2014 - While browsing the log4net source repository, I found that the implementation of LogicalThreadContext was modified in November 2011 to that it stores its properties using CallContext.LogicalSetData (and gets them using LogicalGetData). This is important because that means that LogicalThreadContext should now work correctly. Any data stored in LogicalThreadContext should be "flowed" to any child threads or tasks. This compares to ThreadContext (and the old implementation of LogicalThreadContext) where data stored in the context would remain local to the current thread and NOT flowed to child threads/tasks.

如果你有兴趣,这里的变化:

If you are interested, here is the change:

<一个href="http://svn.apache.org/viewvc/logging/log4net/trunk/src/log4net/Util/LogicalThreadContextProperties.cs?r1=1165341&r2=1207948&diff_format=h">http://svn.apache.org/viewvc/logging/log4net/trunk/src/log4net/Util/LogicalThreadContextProperties.cs?r1=1165341&r2=1207948&diff_format=h

希望有人发生后,这个老问题,会发现这些信息很有用。

Hopefully someone happening upon this old question will find this information useful.

log4net的提供了两种不同的线程上下文对象: ThreadContext 和<一href="http://logging.apache.org/log4net/release/sdk/log4net.LogicalThreadContext.html">LogicalThreadContext,每一个都具有一个属性包,属性。 ThreadContext有一个<一个href="http://logging.apache.org/log4net/release/sdk/log4net.Util.ThreadContextProperties.html">ThreadContextProperties包边LogicalThreadContext有一个<一个href="http://logging.apache.org/log4net/release/sdk/log4net.Util.LogicalThreadContextProperties.html">LogicalThreadContextProperties囊中。

log4net provides two different "thread context" objects: ThreadContext and LogicalThreadContext, each of which has a property bag, Properties. ThreadContext has a ThreadContextProperties bag while LogicalThreadContext has a LogicalThreadContextProperties bag.

ThreadContext也许是更通常称为MDC。 LogicalContext或许更通常称为LDC。 我将使用简称为这篇文章的其余部分。

ThreadContext is perhaps more commonly known as "MDC". LogicalContext is perhaps more commonly known as "LDC". I will use the short name for the rest of this post.

MDC.Properties使用实施<一个href="http://msdn.microsoft.com/en-us/library/system.threading.thread.setdata.aspx">System.Threading.Thread.SetData而LDC.Properties使用使用实施<一个href="http://msdn.microsoft.com/en-us/library/system.runtime.remoting.messaging.callcontext.setdata.aspx">System.Runtime.Remoting.Messaging.CallContext.SetData.

MDC.Properties is implemented using System.Threading.Thread.SetData while LDC.Properties is implemented using using System.Runtime.Remoting.Messaging.CallContext.SetData.

为了比较,NLOG只公开MDC(现称MappedDiagnosticContext)来存储线程​​本地属性。 NLOG的实现使用System.Threading.Thread.SetData,所以它的实现是相同的log4net的

For comparison, NLog only exposes "MDC" (now known as MappedDiagnosticContext) to store thread local properties. NLog's implementation uses System.Threading.Thread.SetData, so its implementation is the same as log4net's.

在两者的log4net和NLOG中,MDC属性存储在字典中它本身存储在线程局部存储器中。

In both log4net and NLog, the "MDC" properties are stored in a dictionary which is itself stored in the thread local storage.

在这样的情况下,将存储在装饰着[ThreadStatic]一类的成员变量的字典已经等同?

In a case like this, would storing the dictionary in a class member variable decorated with [ThreadStatic] have been equivalent?

[ThreadStatic]
private static IDictionary<string, string> threadProperties;

什么是使用.NET 4.0的新的ThreadLocal类中的等价(或相似)的声明?

What be the equivalent (or similar) declaration using .NET 4.0's new ThreadLocal class?

最后,什么是最不发达国家和MDC之间的真正的,实际的,区别是什么?即使阅读了MSDN主题上面链接,这是我不清楚。当你真的用一个比其他?这似乎是的,我看到的log4net的和上下文的参照/例,绝大多数是GDC(全球 - 我的理解),NDC(嵌套 - 这我也理解)和MDC。大多数时候使用Google都与检入到log4net的源$ C ​​$ C库,而不是实际使用的参考,我可以找到最不发达国家(或LogicalThreadContext)的。最不发达国家几乎从未出现在问题或例子。

Ultimately, what is the real, practical, difference between LDC and MDC? Even after reading the MSDN topics linked above, it is not clear to me. When would you really use one over the other? It seems like the vast majority of the references/examples that I see for log4net and context is for GDC (global - which I understand), NDC (nested - which I also understand), and MDC. Most of the references that I can find to LDC (or LogicalThreadContext) when googling are related to checkins into the log4net source code repositories, not real-world usage. LDC almost never comes up in questions or examples.

我没有找到<一href="http://www.l4ndash.com/Log4NetMailArchive%2Ftabid%2F70%2Fforumid%2F1%2Fpostid%2F15288%2Fview%2Ftopic%2FDefault.aspx">this链接提供了有关从一个的log4net的开发商,Nicko Cadell差别一些pretty的良好的信息,但它仍然是我也不清楚。

I did find this link that offers some pretty good information about the difference from one of the log4net developers, Nicko Cadell, but it is still not clear to me.

有一个更大的问题,并不直接涉及log4net的是什么是Thread.SetData和CallContext.SetData之间的实际区别?

A larger question, not directly related to log4net is what is the practical difference between Thread.SetData and CallContext.SetData?

根据<一href="http://msdn.microsoft.com/en-us/library/system.runtime.remoting.messaging.callcontext.aspx">CallContext MSDN文章,CallContext中的数据可以传播到另一个AppDomain中。要传播中,存储在CallContext中一个数据项必须露出<一href="http://msdn.microsoft.com/en-us/library/system.runtime.remoting.messaging.ilogicalthreadaffinative.aspx">ILogicalThreadAffinative接口。所以,这似乎是Thread.SetData和CallContext中区别之一

According to the CallContext MSDN article, CallContext data can be propagated to another AppDomain. To be propagated, a data item stored in the CallContext must expose the ILogicalThreadAffinative interface. So, that seems to be one difference between Thread.SetData and CallContext.

据的Nicko Cadell链路,log4net的不实现ILogicalThreadAffinative,所以土发性质将不会传播。

According the Nicko Cadell link, log4net does not implement ILogicalThreadAffinative, so the LDC properties will not be propagated.

也许有足够的在这里,我应该能够回答我的问题,也许不是。我仍然在努力理解。

Maybe there is enough here that I should be able answer my own question, maybe not. I am still working on understanding.

如果您使用log4net的,你每使用MDC,最不发达国家,两者兼而有之?如果使用MDC,是因为大部分的现实世界的例子似乎都使用它?如果您使用的最不发达国家,你有一个特殊原因使用它?如果同时使用,你如何选择时使用的?

If you use log4net, do you every use MDC, LDC, both? If you use MDC, is it because most of the "real world" examples seem to use it? If you use LDC, to you have a specific reason for using it? If you use both, how do you choose when to use which?

请注意,我已经看到了关于MDC(也许最不发达国家)的一些条款也许不工作的权利在适当的线程切换ASP.net应用。我没有在这个问题特别感兴趣,因为我不工作的ASP.net。

Note that I have seen some articles regarding MDC (and maybe LDC) maybe not working right in ASP.net applications due to thread switching. I am not particularly interested in this problem as I am not working in ASP.net.

其实,我已经找到了几个有用的帖子在这里,这样可能有助于讨论:

Actually, I have found a couple of useful posts here on SO that might contribute to the discussion:

<一个href="http://stackoverflow.com/questions/175129/what-are-best-practices-for-using-thread-local-storage-in-net">What对于使用线程本地存储在.NET中的最佳实践?

净:逻辑线程和线程本地存储

在此先感谢!

推荐答案

警告:这是猜测

假设你正在编写一个服务器,服务请求意味着你必须跟一堆不同的服务。作为一个完全现代化的开发者,您提出这些要求不同步,协调的时候一切都回复(或超时),以原来的要求作出回应。

Suppose you're writing a server, and serving a request means you have to talk to a bunch of different services. Being a thoroughly modern developer, you make these requests asynchronously, coordinating when everything's replied (or timed out) in order to respond to the original request.

这意味着对应于单个请求工作散布许多不同线程(异步处理所述web服务响应)之间。我的犯罪嫌疑人的是 CallContext中用于传播一切我这样做是因为这一个进来的请求,以不同的线程,这样你可以收集所有日志,该请求在一起。 ThreadContext 也帮不了那里。请注意,我假设正在以一个AppDomain中进行的一切工作,让你的关心就不会是一个问题。

That means the work corresponding to a single request is scattered amongst many different threads (processing the web service responses asynchronously). I suspect that CallContext is used to propagate the "everything I'm doing is because of this one incoming request" to different threads, so that you can gather all the logs for that request together. ThreadContext wouldn't help there. Note that I'm assuming all the work is being performed in a single AppDomain, so your concern there wouldn't be a problem.

这篇关于log4net.ThreadContext和log4net.LogicalThreadContext之间的区别是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆