c# - Lazy evaluation with buffers in AdoNetAppender in Log4Net -
c# - Lazy evaluation with buffers in AdoNetAppender in Log4Net -
i'm using log4net custom properties add together environmental info logs. created utility class global accessible properties programme classes utilize store context info (order id, user id, etc) , lazy wrapper around them don't need alter log4net threadcontext time. this:
public class loggerpropertyprovider { private readonly string _value; public loggerpropertyprovider(string value) { _value = value; } public override string tostring() { homecoming _value; } }
whatever value want expose property log4net register using lazy evaluator @ start of application.
threadcontext.properties["order_id"] = new loggerpropertyprovider(contextinformation.orderid);
it works fine buffer-less appenders (such rolling file) or when buffer set 0 in adonetappender. however, when have buffer > 1 log4net defers evaluation of property until buffer flushed @ end of application or when entries in buffer > buffersize.
when happens info not in global property anymore or it's value has been changed (like loop processing orders) wrong or null value in logs.
the alternative can see prepare stop using buffers property value evaluated in calls when entry beingness flushed. since properties in threadcontext evaluated when buffer beingness flushed i'm afraid cannot have different property value each log call.
is there way can create log4net evaluate threadcontext (or other context has) when buffering entry instead of when flushes it?
thanks
by default, log4net not fix
property provider values contexts @ log time buffered appenders. evaluated @ buffer flush time.
to solve issue, have implement ifixingrequired log4net.core namespace.
public class loggerpropertyprovider : ifixingrequired { private readonly string _value; public loggerpropertyprovider(string value) { _value = value; } public override string tostring() { homecoming _value; } object ifixingrequired.getfixedobject() { homecoming tostring(); } }
(tested own property provider, relies on http context due own needs:
// can not utilize log4net threadcontext or logicalthreadcontext asp.net, since // asp.net may switch thread while serving request, , reset phone call context in // process. public class httpcontextvalueprovider : ifixingrequired { private string _contextkey; public httpcontextvalueprovider(string contextkey) { _contextkey = contextkey; } public override string tostring() { var currcontext = httpcontext.current; if (currcontext == null) homecoming null; var value = currcontext.items[_contextkey]; if (value == null) homecoming null; homecoming value.tostring(); } object ifixingrequired.getfixedobject() { homecoming tostring(); } }
general thought coming piers blog. see my other answer question if you'd more date details httpcontextvalueprovider
.)
c# buffer log4net adonetappender
Comments
Post a Comment