Manuel,

 

I am using version 1.2.9.0, I guess the LogicalThreadContext was added after beta8 (I am not sure about this)?

 

I agree on your comment, I would also prefer a solution that didn’t require changing every method that performs logging. The only one I can think of is the one you mention in your first mail (creating non static log objects based on different logger names).

 

 

Best regards

Georg

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

 

 

 

 


From: Manuel Reyes [mailto:[EMAIL PROTECTED]
Sent: 6. januar 2006 11:50
To: Log4NET User
Subject: RE: Using MDC to handle multirequests

 

Georg

 

Thanks for the reply.

 

What version of Log4Net are you using? The release I have (log4net-1.2.0-beta8) does not have the LogicalThreadContext item.

 

That being said the solution you posted does look as if it would work, although it does mean that every method that performs logging will required the “using (..){}” syntax which is something I would prefer not to do.  If possible I am looking for some method of adding the UID to the logger in the “MyFunc” class constructor and then not have to worry about it anywhere else.

 

Manuel

 


From: Georg Jansen [mailto:[EMAIL PROTECTED]
Sent: 05 January 2006 18:28
To: 'Log4NET User'
Subject: RE: Using MDC to handle multirequests

 

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 - Log4Net Dashboard / Log Viewer

 

 

 

 

-----Original Message-----
From: Manuel Reyes [mailto:[EMAIL PROTECTED]
Sent: 5. januar 2006 18:29
To: [email protected]
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.

Reply via email to