You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4cxx-user@logging.apache.org by Fabian Jacquet <fa...@gmail.com> on 2010/08/18 11:27:42 UTC

MDC automatically inherited

Hi all,

I need to use NDC or MDC. I would like that child thread automatically
inherit the parent context so I try to use MDC.

Here is my code and it seems the context is not inherited:

#include "stdafx.h"
#include <log4cxx/log4cxx.h>
#include <log4cxx/logger.h>
#include <log4cxx/logmanager.h>
#include <log4cxx/xml/domconfigurator.h>

DWORD WINAPI test1(LPVOID param)
{
    log4cxx::LoggerPtr log = log4cxx::LogManager::getLogger("MyLogger");
    LOG4CXX_INFO(log, "Message thread with MDC");

    return 0;
}

int _tmain(int argc, _TCHAR* argv[])
{
    log4cxx::xml::DOMConfigurator::configure("log.xml");

    log4cxx::LoggerPtr log = log4cxx::LogManager::getLogger("MyLogger");
    LOG4CXX_INFO(log, "Message without MDC");

    log4cxx::MDC::put("MyMDC", "MyMDC");
    LOG4CXX_INFO(log, "Message with MDC");

    CreateThread(NULL,NULL,test1,NULL, 0, NULL);

    Sleep(3000);
    return 0;
}


Output:
2010-08-18 11:27:44,015 [0x0000073c] INFO   {MyLogger - ::__cdecl wmain(24)}
- Message without MDC
2010-08-18 11:27:44,015 [0x0000073c] INFO  MyMDC {MyLogger - ::__cdecl
wmain(27)} - Message with MDC
2010-08-18 11:27:44,015 [0x00000bf0] INFO   {MyLogger - ::long __stdcall
test1(14)} - Message thread with MDC


I don't know where is my mistake.

Re: MDC automatically inherited

Posted by Fabian Jacquet <fa...@gmail.com>.
Bad luck

Thank you.

2010/8/20 Krzysztof Bieleń <kz...@gmail.com>

> Hi,
>
> it seems that this bug was reported earlier:
>
> https://issues.apache.org/jira/browse/LOGCXX-339
>
> And here is copy of Curt Arnold answer to this issue:
> > That statement was likely copied over verbatim from log4j and then never
> implemented. I'm guessing it may not be implementable, at least without
> platform-specific code. Open to implementation suggestions.
>
> Unfortunately, you have to copy MDC context to child threads by yourself.
>
> --
> Krzysztof Bieleń
>

Re: MDC automatically inherited

Posted by Krzysztof Bieleń <kz...@gmail.com>.
Hi,

it seems that this bug was reported earlier:

https://issues.apache.org/jira/browse/LOGCXX-339

And here is copy of Curt Arnold answer to this issue:
> That statement was likely copied over verbatim from log4j and then never
implemented. I'm guessing it may not be implementable, at least without
platform-specific code. Open to implementation suggestions.

Unfortunately, you have to copy MDC context to child threads by yourself.

--
Krzysztof Bieleń

Re: MDC automatically inherited

Posted by Fabian Jacquet <fa...@gmail.com>.
does somebody use MDC with success?

On Wed, Aug 18, 2010 at 11:27, Fabian Jacquet <fa...@gmail.com>wrote:

> Hi all,
>
> I need to use NDC or MDC. I would like that child thread automatically
> inherit the parent context so I try to use MDC.
>
> Here is my code and it seems the context is not inherited:
>
> #include "stdafx.h"
> #include <log4cxx/log4cxx.h>
> #include <log4cxx/logger.h>
> #include <log4cxx/logmanager.h>
> #include <log4cxx/xml/domconfigurator.h>
>
> DWORD WINAPI test1(LPVOID param)
> {
>     log4cxx::LoggerPtr log = log4cxx::LogManager::getLogger("MyLogger");
>     LOG4CXX_INFO(log, "Message thread with MDC");
>
>     return 0;
> }
>
> int _tmain(int argc, _TCHAR* argv[])
> {
>     log4cxx::xml::DOMConfigurator::configure("log.xml");
>
>     log4cxx::LoggerPtr log = log4cxx::LogManager::getLogger("MyLogger");
>     LOG4CXX_INFO(log, "Message without MDC");
>
>     log4cxx::MDC::put("MyMDC", "MyMDC");
>     LOG4CXX_INFO(log, "Message with MDC");
>
>     CreateThread(NULL,NULL,test1,NULL, 0, NULL);
>
>     Sleep(3000);
>     return 0;
> }
>
>
> Output:
> 2010-08-18 11:27:44,015 [0x0000073c] INFO   {MyLogger - ::__cdecl
> wmain(24)} - Message without MDC
> 2010-08-18 11:27:44,015 [0x0000073c] INFO  MyMDC {MyLogger - ::__cdecl
> wmain(27)} - Message with MDC
> 2010-08-18 11:27:44,015 [0x00000bf0] INFO   {MyLogger - ::long __stdcall
> test1(14)} - Message thread with MDC
>
>
> I don't know where is my mistake.
>