Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我知道 MDC 用于客户端服务器,进行日志记录以找出哪个日志用于哪个客户端。(参考)
但是我们为什么使用MDC.remove()?
MDC.remove()
是内存效率问题吗?
另一个原因是该值已过时。例如,如果您在 MDC 中有一个用户名,但该用户已注销,则您的信息已过时。如果您仍然记录它,它可能会完全误导调试。
通常,put() 操作应该由相应的 remove() 操作来平衡。否则,MDC 将包含某些键的陈旧值。我们建议尽可能在 finally 块中执行 remove() 操作,以确保无论代码的执行路径如何都能调用它们。