Bug 5932 - MDC is not working with AsyncAppender
Summary: MDC is not working with AsyncAppender
Status: RESOLVED FIXED
Alias: None
Product: Log4j - Now in Jira
Classification: Unclassified
Component: Other (show other bugs)
Version: unspecified
Hardware: All All
: P3 normal
Target Milestone: ---
Assignee: log4j-dev
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2002-01-21 00:22 UTC by Heikki Linnakangas
Modified: 2004-11-16 19:05 UTC (History)
0 users



Attachments

Note You need to log in before you can comment on or make changes to this bug.
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.