c# - MemoryAppender.GetEvents : Events Null -


can explain why not getting events memoryappender? in other words, events variable null.

    public void log(string message, category category, priority priority)     {         memoryappender memoryappender = new memoryappender();         log4net.config.xmlconfigurator.configure(new system.io.fileinfo(@"c:\users\username\documents\github\massspecstudio\massspecstudio 2.0\source\massspecstudio\core\app.config"));         bool log4netisconfigured = log4net.logmanager.getrepository().configured;         switch(category)         {             case category.debug:                 log.debug(message);                 break;             case category.warn:                 log.warn(message);                 break;             case category.exception:                 log.error(message);                 break;             case category.info:                 log.info(message);                 break;         }         var events = memoryappender.getevents(); // events null.         int esize = events.length;         foreach (loggingevent loggingevent in events)         {             logitem logitem = new logitem(loggingevent.timestamp, loggingevent.level, loggingevent.renderedmessage);             logitems.add(logitem);         }     } 

*update:

i have provided following in config file. how isn't memoryappender hooking logger properly?

<log4net> <appender name="logfileappender" type="log4net.appender.rollingfileappender">   <file type="log4net.util.patternstring" value="c:\temp\logger_.txt" />   <param name="appendtofile" value="true"/>   <param name="rollingstyle" value="once"/>   <param name="rollingstyle" value="date"/>   <datepattern value="yyyy-mm-dd_hhmmss" />   <lockingmodle type="log4net.appender.fileappender+minimallock"/>   <preservelogfilenameextension value="true"/>   <maxsizerollbackups value="30" />   <staticlogfilename value="false" />   <layout type="log4net.layout.patternlayout">     <param name="conversionpattern" value="%date [%thread] %-5level %logger - %message%newline" />   </layout> </appender> <appender name="memoryappender" type="log4net.appender.memoryappender">   <layout type="log4net.layout.patternlayout">     <conversionpattern value="%date [%thread] %-5level %message%newline" />   </layout> </appender> <root>   <level value="all" />   <appender-ref ref="logfileappender" />   <appender-ref ref="memoryappender" /> </root> 

log4net works on 'loggers' , 'appenders'. need connect appender logger in order receive messages being logged. can done in code or in config file. appear doing bit of both, initialising log4net using config file, creating memoryappender in code.

problems:

  1. you're initialising log4net every time function called. need once per process.
  2. you have created new instance of memoryappender every call function, when probable want one.
  3. the main issue: memoryappender not hooked logger. if want in code, see here: programmatically adding , removing log appenders in log4net

updated answer revised question:

to events single memoryappender in .config file:

var appender = log4net.logmanager.getrepository().getappenders().oftype<log4net.appender.memoryappender>().single(); // assumes 1 memoryappender var events = appender.getevents(); 

Comments

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -