Bug 5932

Summary: MDC is not working with AsyncAppender
Product: Log4j - Now in Jira Reporter: Heikki Linnakangas <heikki.linnakangas>
Component: OtherAssignee: log4j-dev <log4j-dev>
Status: RESOLVED FIXED    
Severity: normal    
Priority: P3    
Version: unspecified   
Target Milestone: ---   
Hardware: All   
OS: All   

Description Heikki Linnakangas 2002-01-21 00:22:52 UTC
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.
Comment 1 Ceki Gulcu 2002-04-30 14:46:42 UTC
Hello Heikki,

Thank you for the clear bug report. Your fix has been incorporated and will
ship with log4j 1.2 final.