c# - Lazy evaluation with buffers in AdoNetAppender in Log4Net -


I am using Log4Net custom property which is to add some environmental information to my logs. Has created a utility class which is used by my program classes to store reference information (order id, user id, etc.) and lazy wrap around them, so I do not need to change Log4Net thread concert all the time is. Something like this:

  public class LoggerPropertyProvider {private redone string _value; Public LogarProperty Provider (String Value) {_value = value; } Public override string toasting () {return _value; }}  

Whatever value I want to expose to Log4Net as a property, I use this lazy evaluator in the beginning of this Register me

  ThreadContext.Properties ["ORDER_ID"] = New LoggerPropertyProvider (ContextInformation.OrderId) =;  

OK buffer works with fewer appenders (such as a rolling file) or when the buffer is set to the AdonetTapender in 0. However, when i buffer> 1 def44 evaluates the value of the property until the buffer is flushed at the end of the application or when entries in the buffer> bufferSize .

When this happens, the information is not in the global property or its value is changed (such as the loop processing order) so I get incorrect or zero value in my log.

The only option I can see to fix this is to stop using buffers so that the value of the property is evaluated in all calls when the entry is being flushed. Since the property is evaluated in ThreadContext when the buffer is being flushed I am afraid that I can not have the value of a different property for every log call.

Is there any way I can log4net to evaluate a ThreadContext or (in some other context) when it buffers the entry instead of flush it?

Thanks

By default, log4net buffered references to log time for the appender Property provider does not value from To evaluate the buffer flush time,

, you apply the IFixingRequired from the log4net.Core namespace

  the public class LoggerPropertyProvider to solve this problem. IFixingRequired {Private string-_value read-only; Public LogarProperty Provider (String Value) {_value = value; } Public override string toasting () {return _value; } Object IFixingRequired.GetFixedObject () {ToString ()) back; }}  

(Tested with my own property provider, which depends on the http context due to my own requirements:

  // We can not use log4net ThreadConnect or as LogicalThreadContext with ASP.net, while requesting the // can switch the asp.net thread, and // process the call reference reset public class HttpContextValueProvider: IFixingRequired {private} String _contextKey; public HttpContextValueProvider (string contextKey) {_contextKey = ContextKey;} public override string ToString () {var currContext = HttpContext.Current; if (currContext == faucet) return null; value = currContext.Items [_contextKey]; if (value == faucet) return null, return value. Trusting ();} Object IFixingRequired.GetFixedObject () {ToString ()}}}  

come up with a general idea. Check it out on another question if you want to update more about Information in this HttpContextValueProvider .)


Comments