c# - How can I programmatically update log4net configuration file? -


i have log4net config in applications' app.config file, such:

...     <section name="log4net" type="log4net.config.log4netconfigurationsectionhandler, log4net"/>   </configsections>   <log4net>     <appender name="rollinglogfileappender" type="log4net.appender.rollingfileappender">       <file value="logs\service.log"/>       <appendtofile value="true"/>       <rollingstyle value="date"/>       <datepattern value="yyyymmdd"/>       <layout type="log4net.layout.patternlayout">         <conversionpattern value="%date [%thread] %-5level %logger - %message%newline"/>       </layout>     </appender>     <appender name="consoleappender" type="log4net.appender.consoleappender">       <layout type="log4net.layout.patternlayout">         <conversionpattern value="%date [%thread] %-5level %logger - %message%newline"/>       </layout>     </appender>     <logger name="data.webservice">       <level value="all"/>       <appender-ref ref="consoleappender"/>       <appender-ref ref="rollinglogfileappender"/>     </logger>     <logger name="data.host.hostservice">       <level value="all"/>       <appender-ref ref="consoleappender"/>       <appender-ref ref="rollinglogfileappender"/>     </logger>   </log4net> 

i know can read in via log4net.config.xmlconfigurator.configure();, however, able update via sort of call well. i'll accepting configuration web service and, once i've set new config (currently log level, i'm not precluding other things being configurable down road), need update in config file.

having of configs in 1 file convenient, however, i'm open locating config in file if makes simpler.

since there no official method so, wound writing method uses xpath locate element(s) change , update accordingly. works enough need , more elegant brute-force "readinthefiletoastringthenreplacethetextthensavethestringtothefile" approach.

    public enum log4netconfigitem     {         loglevel     }      public const string log4net_configfile = "log4net.config";      public void updateconfiguration(log4netconfigitem which, object value)     {         // load config file.         xmldocument doc = new xmldocument();         doc.load(log4net_configfile);         // create xpath navigator document.         xpathnavigator nav = doc.createnavigator();          try         {             xpathexpression expr;              // compile correct xpath expression element want configure.             switch (which)             {                 default:                 case log4netconfigitem.loglevel:                     // compile standard xpath expression                     expr = nav.compile("/configuration/log4net/logger/level");                     break;             }              // locate node(s) defined xpath expression.             xpathnodeiterator iterator = nav.select(expr);              // iterate on node set             while (iterator.movenext())             {                 xpathnavigator nav2 = iterator.current.clone();                  // update element required.                 switch (which)                 {                     default:                     case log4netconfigitem.loglevel:                         // update 'value' attribute log level.                         setattribute(nav2, string.empty, "value", nav.namespaceuri, value.tostring());                         break;                 }             }              // save modified config file.             doc.save(log4net_configfile);         }         catch (exception ex)          {             console.writeline(ex.message);         }     }      void setattribute(system.xml.xpath.xpathnavigator navigator, string prefix, string localname, string namespaceuri, string value)     {         if (navigator.canedit == true)         {             // check if given localname exist             if (navigator.movetoattribute(localname, namespaceuri))             {                 // exist, set current attribute new value.                 navigator.setvalue(value);                 // move navigator beginning of node                 navigator.movetoparent();             }             else             {                 // not exist, create new attribute                 navigator.createattribute(prefix, localname, namespaceuri, value);             }         }     } 

note: setattribute code got here.


Comments

Popular posts from this blog

delphi - How to convert bitmaps to video? -

jasper reports - Fixed header in Excel using JasperReports -

python - ('The SQL contains 0 parameter markers, but 50 parameters were supplied', 'HY000') or TypeError: 'tuple' object is not callable -