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.