You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4net-user@logging.apache.org by Manuel Reyes <Ma...@e-mis.com> on 2006/01/05 18:29:29 UTC

Using MDC to handle multirequests

Hello List,

I am currently trying to get my head around using MDC to handle logging
in an environment with multiple simultaneous requests.  Another reason
for using MDC is that I need to use the "%X{MyParameter}" syntax in my
conversion pattern.

The problem I have is that (by Log4Net design) the MDC parameters are
overwritten by new parameters. As an example, I have three assemblies
"Caller", "Functions" and "Log4Net". Caller has the following basic code
and performs no logging:

//---------
Functions f1 = new Functions("uniqueID_1")
Functions f2 = new Functions("uniqueID_2")
f1.DoRandomThing()
f2.DoRandomThing()
f1.DoRandomThing()
f2.DoRandomThing()
//---------

The Functions assembly (which does the logging) references the Log4Net
assembly as Logger and contains the following code in its constructor:

//set the UID from the constructor parameter
Logger.MDC.Set("UID", uniqueID);

And this code in the DoRandomThing() function

//output info log
log.Info("hello from dorandomthing");

Using "%X{UID}" in my conversion pattern I was hoping to get the
following log output:

uniqueID_1
uniqueID_2
uniqueID_1
uniqueID_2

But I actually get:

uniqueID_2
uniqueID_2
uniqueID_2
uniqueID_2

I realise that the reason for this is that the MDC class is static, so I
am looking for alternative.  Therefore if anybody has any suggestions to
assigning a unique ID per instance please post them up.

Thanks in advance
Manuel

p.s. one thing I did consider was using the unique ID as the logger name
:

//set logger name
this.log = Logger.LogManager.GetLogger(typeof(Functions) + uniqueID);

But this is a tad messy and difficult to manage in code should I need to
amend it.

RE: Using MDC to handle multirequests

Posted by Georg Jansen <Ge...@FaktNet.com>.
Manuel,

 

Will something like this solve it?

 

public class myFunc

{

    private static readonly log4net.ILog log =
log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod()
.DeclaringType);

    private string mUid;

 

    public myFunc(string uniqueId)

    {

       mUid = uniqueId;

    }

 

    public void DoSomthing()

    {

        using (log4net.LogicalThreadContext.Stacks["UID"].Push(mUid))

        {

            // do somthing ....

            log.Debug("Hello");

        }

    }

}

 

 

Best regards

Georg

http://www.l4ndash.com <http://www.l4ndash.com/>  - Log4Net Dashboard / Log
Viewer

 

 

 

 

-----Original Message-----
From: Manuel Reyes [mailto:Manuel.Reyes@e-mis.com] 
Sent: 5. januar 2006 18:29
To: log4net-user@logging.apache.org
Subject: Using MDC to handle multirequests

 

Hello List,

 

I am currently trying to get my head around using MDC to handle logging

in an environment with multiple simultaneous requests.  Another reason

for using MDC is that I need to use the "%X{MyParameter}" syntax in my

conversion pattern.

 

The problem I have is that (by Log4Net design) the MDC parameters are

overwritten by new parameters. As an example, I have three assemblies

"Caller", "Functions" and "Log4Net". Caller has the following basic code

and performs no logging:

 

//---------

Functions f1 = new Functions("uniqueID_1")

Functions f2 = new Functions("uniqueID_2")

f1.DoRandomThing()

f2.DoRandomThing()

f1.DoRandomThing()

f2.DoRandomThing()

//---------

 

The Functions assembly (which does the logging) references the Log4Net

assembly as Logger and contains the following code in its constructor:

 

//set the UID from the constructor parameter

Logger.MDC.Set("UID", uniqueID);

 

And this code in the DoRandomThing() function

 

//output info log

log.Info("hello from dorandomthing");

 

Using "%X{UID}" in my conversion pattern I was hoping to get the

following log output:

 

uniqueID_1

uniqueID_2

uniqueID_1

uniqueID_2

 

But I actually get:

 

uniqueID_2

uniqueID_2

uniqueID_2

uniqueID_2

 

I realise that the reason for this is that the MDC class is static, so I

am looking for alternative.  Therefore if anybody has any suggestions to

assigning a unique ID per instance please post them up.

 

Thanks in advance

Manuel

 

p.s. one thing I did consider was using the unique ID as the logger name

:

 

//set logger name

this.log = Logger.LogManager.GetLogger(typeof(Functions) + uniqueID);

 

But this is a tad messy and difficult to manage in code should I need to

amend it.