MDC seems to work inproperly with AsyncAppender. I have a custom appender that reads some entries from the MDC, and writes them to a database. If I use it with the AsyncAppender, the MDC information is sometimes wrong. The problems seems to be that LoggingEvent.getMDCCopy() doesn't actully create a copy of the hashtable, but just gets a reference to it. If you apply the patch below... diff -b -Naur orig/org/apache/log4j/spi/LoggingEvent.java patched/org/apache/log 4j/spi/LoggingEvent.java --- orig/org/apache/log4j/spi/LoggingEvent.java Mon Jan 21 10:08:10 2002 +++ patched/org/apache/log4j/spi/LoggingEvent.java Mon Jan 21 10:07:44 2002 @@ -200,7 +200,7 @@ void getMDCCopy() { if(mdcLookupRequired) { ndcLookupRequired = false; - mdcCopy = MDC.getContext(); + mdcCopy = (Hashtable)MDC.getContext().clone(); } } ... it starts working. However, there will still be problems, if you put mutable objects in the MDC, since Hashtable.clone makes shallow clones.
Hello Heikki, Thank you for the clear bug report. Your fix has been incorporated and will ship with log4j 1.2 final.