删除 ThreadContext 属性
我已经设置了 ThreadContext.Properties,并且需要在代码的生命周期中更新其值。过去我使用过 log4j MDC 并且必须: MDC.删除(跟踪);
然后通过以下方式添加另一个值: MDC.put(跟踪,trackingIdStr);
现在我正在使用 Log4Net,我们的应用程序使用属性: log4net.ThreadContext.Properties["TrackingId"] = tracker;
问题:如何删除以前的值并添加新值?是不是很简单: log4net.ThreadContext.Properties["TrackingId"] = tracker2;
I have set the ThreadContext.Properties and need to update its value during the life-cycle of my code. In the past I have used log4j MDC and had to:
MDC.remove(TRACKING);
and then add another value by:
MDC.put(TRACKING, trackingIdStr);
Now that I am using Log4Net, our app uses Properties:
log4net.ThreadContext.Properties["TrackingId"] = tracker;
Question: How do I remove the previous value and add a new value? Is it as easy as just:
log4net.ThreadContext.Properties["TrackingId"] = tracker2;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,就是这么简单。 您可以按照问题中所示重新分配,也可以在完成后完全删除该值。
如果您希望上下文属性对特定代码段有效(通过使用),那么您可以尝试 ThreadContext.Stacks:
Yes, it is that easy. You can reassign as you have shown in your question or you can remove the value altogether when you are finished with it.
If you want your context properties to be in effect for a specific section of code (via using) then you can try ThreadContext.Stacks:
也许这并不能直接回答您的问题,但在您的情况下,您可能会考虑使用计算出的上下文值。这里有很好的解释:
Log4Net 教程第 6 部分:记录事件上下文
(向下滚动一点)
Maybe this does not directly answer your question, but in your situation you might consider to use calculated context values. This is explained nicely here:
Log4Net Tutorial pt 6: Log Event Context
(scroll down a bit)