Question

I am using Log4Net for WCF service. I want a custom parameter "REQUESTID" to be passed to log4net.

log4net patternlayout as shown below:

<layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%date %-5level %-25aspnet-context{REQUESTID} %message%newline" />

</layout>

I tried below:

OperationContext.Current.RequestContext.RequestMessage.Properties.Add("REQUESTID ",10001);

But its not working as expected. Any ideas would be appreciated.

Was it helpful?

Solution 2

I figured out that aspnet-context does not work in this case.

So we have to use context properties as shown below:

//log4net parameter for RequestId
log4net.GlobalContext.Properties["RequestId"] = requestId;

web.config change:

<conversionPattern value="%date %-5level %-25%property{RequestId} %message%newline" />

Hope this helps someone looking for this.

OTHER TIPS

Your close, but not quite there. See the section "Context Properties" in the following link on how to achieve this.

http://logging.apache.org/log4net/release/manual/contexts.html

Also note that you are going to want to do this before calling XmlConfigurator.Configure();

HTH, Eric

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top