multithreading - Log4Net C# logging issue with multi-thread code execution -
i using log4net
log exception in .net windows service
application.
i want mention that, windows service application running multi-threading execution
, means each task being processed on each different thread , have used delegate - (begininvoke)
pattern.
importantly, using dynamic properties
generate multiple log files dynamically log4net
based on application's different scenarios.
now, on every logging/ exception
scenarios (in c# method), have used logger.log
method log info/ exception log4net
.
code (dynamic file generation)
globalcontext.properties[filenameparameter] = directoryname + filename; logmanager.info(logmessage);
config settings
<appender name="rollingfileappender" type="log4net.appender.rollingfileappender"> <file type="log4net.util.patternstring" value="d:\data\%property{filename}_info.log"/> </appender>
the problem (i believe), due multi-threaded code execution, getting following things log4net weird.
- sometime log file generates, no message content.
- sometime file not generating.
- sometime few file generates, few not generating.
would please let me know why log4net behaves this. need stable logging multi-threading code execution.
thanks in advance!
i haven't tried doing, can see 2 problems code cause problems. (i wouldn't expect logging information lost, expect information go wrong file).
firstly, should using threadcontext rather globalcontext if property expected change on different threads. issue trivial fix.
the second issue may more problematic. log4net not expect base filename change during program execution , may continue write old filename after have changed property.
one of easiest ways around re-initialize logger. if using xml configuration app.config, can call log4net.config.xmlconfigurator.configure();
after have set property. has overhead , have decide whether overhead acceptable.
Comments
Post a Comment