我已经设置了ThreadContext.Properties,并且需要在代码的生命周期中更新它的值。在过去,我使用过log4j MDC,而且不得不这样做:MDC.remove(跟踪);
然后添加另一个值: MDC.put(TRACKING,trackingIdStr);
现在我使用了跟踪器,我们的应用程序使用了属性: log4net.ThreadContext.Properties"TrackingId“= Log4Net;
问:如何删除前一个值并添加一个新值?是不是就像这样简单: log4net.ThreadContext.Properties"TrackingId“= tracker2;
发布于 2011-10-01 10:52:43
是的,就是这么简单。“您可以按照问题中的说明进行重新分配,也可以在完成后完全删除该值。”
//set
ThreadContext.Properties["TrackingId"] = tracker1;
//reset
ThreadContext.Properties["TrackingId"] = tracker2;
//completely remove
ThreadContext.Properties.Remove("TrackingId");如果你想让你的上下文属性对特定的代码段生效(通过使用),那么你可以尝试ThreadContext.Stacks:
using(ThreadContext.Stacks["TrackingId"].Push("hello"))
{
//messages logged here will be tagged with TrackingId="hello"
}
//messages logged here will not be tagged with TrackingId="hello"发布于 2011-10-05 15:27:51
https://stackoverflow.com/questions/7613654
复制相似问题