Thank you for your help Rob.  We identified that if one of the custom 
properties was not set at the point in time we're about to log a message (in 
our case not all log messages had a job id), that the value that was previously 
set for that thread was logged.  This was the cause of our inconsistencies.  So 
far we seem to be fine as long as we explicitly set all custom properties every 
log attempt including nullifying values if we don't expect one.

I'm still not 100% confident in our solution at this point but we've made some 
ground anyways.  Thanks again for your thoughts.

From: Rob Prouse [mailto:rob.pro...@ivara.com]
Sent: Wednesday, September 01, 2010 1:42 PM
To: Log4NET User
Subject: RE: Custom Properties in an ASP.Net Web Application

I took a quick look at the code and as far as I can see, the ThreadContext is 
taken out of the logging event when formatting the message which happens when 
it is being written. Therefore, I believe that if you use any appender deriving 
from the BufferingSkeletonAppender, then the ThreadContext will be wrong 
because the writing of the message happens on a different thread. So, I don't 
think you can use the ThreadContext.

I assume you want to use properties so that you can have them as separate 
columns in the DB, otherwise you could just insert the values into the message. 
If that is the case, then the only solution I can think of is to download the 
source, copy the code from the ADO appender and create your own that does not 
derive from the buffering appender.

Or, you could look at NLog and see if it meets your needs.

Rob

From: Gagne, Chris [mailto:cga...@ivey.uwo.ca]
Sent: Wednesday, September 01, 2010 12:01 PM
To: 'Log4NET User'
Subject: RE: Custom Properties in an ASP.Net Web Application

Yeah we're using the log4net.Appender.AdoNetAppender.

From: Rob Prouse [mailto:rob.pro...@ivara.com]
Sent: Wednesday, September 01, 2010 11:59 AM
To: Log4NET User
Subject: RE: Custom Properties in an ASP.Net Web Application

For the common log appenders, the log is output synchronously with the log 
message, so it should be on the same thread, so it shouldn't be a problem. I am 
not sure of the behavior when you use an asynchronous appender (like the ADO 
appender), but it should be pulling the context out before it goes 
asynchronous. If it doesn't, then that would be a bug in log4net, but I would 
need to look at the code.

What appender are you using?

From: Gagne, Chris [mailto:cga...@ivey.uwo.ca]
Sent: Wednesday, September 01, 2010 11:03 AM
To: Log4NET User
Subject: Custom Properties in an ASP.Net Web Application

Here's our situation...

We're adding debug messages to an ASP.Net web application that allows our users 
to apply for a job online.  We have a custom properties and database fields 
created named JobPostingId and MemberId.  Here's a small code snippet of our 
logging.

ILog logger = LogManager.GetLogger(CONST_JOBPOSTINGS);

if (logger.IsDebugEnabled)
{
                log4net.ThreadContext.Properties[CONST_JOBPOSTINGID] = 
JobPostingId;
log4net.ThreadContext.Properties[CONST_MEMBERID] = MemberId;
                logger.Debug(message);
}

We've noticed very inconsistent values for our custom properties.  I suspect it 
has something to do with the context belonging to a thread and that same thread 
being reused by different web requests, therefore different users.

Does anyone have suggestions for me on how I can capture user specific data 
into custom properties?  Any thoughts or suggestions would be greatly 
appreciated.

Reply via email to