I'm using Log4Net custom properties to add some environmental information into my logs. I created an utility class with global accessible properties that my program classes use to store context information (order id, user id, etc) and a lazy wrapper around them so I don't need to change Log4Net ThreadContext all the time. Something like this:
public class LoggerPropertyProvider
{
private readonly string _value;
public LoggerPropertyProvider(string value)
{
_value = value;
}
public override string ToString()
{
return _value;
}
}
Whatever value I want to expose as a property to Log4Net I just register using this lazy evaluator at the start of the application.
ThreadContext.Properties["ORDER_ID"] = new LoggerPropertyProvider(ContextInformation.OrderId);
It works just fine with buffer-less appenders (such as rolling file) or when the buffer is set to 0 in AdoNetAppender. However, when I have buffer > 1 Log4Net defers the evaluation of the property until the buffer is flushed at the end of the application or when entries in buffer > bufferSize.
When this happens the information is not in the global property anymore or it's value has been changed (like a loop processing orders) so I get a wrong or null value in my logs.
The only option I can see to fix this is stop using buffers so the property value is evaluated in all calls when the entry is being flushed. Since the properties in ThreadContext are only evaluated when the buffer is being flushed I'm afraid I cannot have a different property value for each log call.
Is there any way I can make Log4Net evaluate a ThreadContext (or some other context it has) when buffering the entry instead of when it flushes it?
Thanks