gpt4 book ai didi

.net - 删除 ThreadContext 属性

转载 作者:行者123 更新时间:2023-12-02 00:29:29 25 4
gpt4 key购买 nike

我已经设置了 ThreadContext.Properties 并且需要在我的代码生命周期中更新它的值。过去我使用过 log4j MDC 并且不得不:MDC.remove(跟踪);

然后通过以下方式添加另一个值:MDC.put(跟踪, trackingIdStr);

现在我正在使用 Log4Net,我们的应用程序使用属性:log4net.ThreadContext.Properties["TrackingId"] = 跟踪器;

问题:如何删除以前的值并添加新值?是不是就这么简单:log4net.ThreadContext.Properties["TrackingId"] = tracker2;

最佳答案

是的,就这么简单。您可以按照问题中所示重新分配,也可以在完成后完全删除该值。

//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"

关于.net - 删除 ThreadContext 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7613654/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com