views:

62

answers:

1

I have a framework that uses log4net for logging, I know that we can disable or enable the desired logging in log4net via the config file.I want to develop some settings class so that the admin or user can set the logging etc.. What is the class etc in log for net to get the config file and change the settings in the config file via code? (are there any or should I use .net System.Xml) Any code snippets?

<?xml version="1.0" encoding="utf-8" ?>
<log4net>

  <root>
    <level value="ALL" />
    <!--<appender-ref ref="LogFileAppender" />-->
    <!--<appender-ref ref="ConsoleAppender" />-->
    <appender-ref ref="DEBUG-RollingLogFileAppender" />
    <appender-ref ref="Error-RollingLogFileAppender" />
  </root>

  <appender name="LogFileAppender" type="log4net.Appender.FileAppender" >
    <param name="File" value="log-file.txt" />
    <param name="AppendToFile" value="true" />
    <layout type="log4net.Layout.PatternLayout">
      <param name="Header" value="[Header]\r\n"/>
      <param name="Footer" value="[Footer]\r\n"/>
      <param name="ConversionPattern" value="%d [%t] %-5p %c %ndc %P{auth} - %m%n" />
    </layout>
  </appender>

  <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender" >
    <layout type="log4net.Layout.PatternLayout">
      <param name="ConversionPattern" value="%d [%t] %-5p %c %ndc %P{auth} - %m%n" />
    </layout>
  </appender>

  <appender name="DEBUG-RollingLogFileAppender" type="log4net.Appender.RollingFileAppender,log4net">
    <param name="File" value="..\\logs\\debug" />
    <param name="AppendToFile" value="true" />
    <param name="StaticLogFileName" value="false" />
    <param name="RollingStyle" value="Composite" />
    <param name="DatePattern" value=".yyyyMMdd'.log'" />
    <param name="MaxSizeRollBackups" value="30" />
    <param name="MaximumFileSize" value="5MB" />
    <layout type="log4net.Layout.PatternLayout,log4net">
      <!--<param name="ConversionPattern" value="%d [%t] %-5p %c %ndc %P{auth} - %m%n" />-->
      <param name="ConversionPattern" value="%d [%t] %-5p - %m%n" />
    </layout>
    <filter type="log4net.Filter.LevelRangeFilter">
      <!--<levelMin value="INFO" />-->
      <levelMax value="INFO" />
    </filter>
  </appender>

  <appender name="Error-RollingLogFileAppender" type="log4net.Appender.RollingFileAppender,log4net">
    <param name="Threshold" value="WARN"/>
    <param name="File" value="..\\logs\\errors" />
    <param name="AppendToFile" value="true" />
    <param name="StaticLogFileName" value="false" />
    <param name="RollingStyle" value="Composite" />
    <param name="DatePattern" value=".yyyyMMdd'.log'" />
    <param name="MaxSizeRollBackups" value="30" />
    <param name="MaximumFileSize" value="5MB" />
    <layout type="log4net.Layout.PatternLayout,log4net">
        <header value="&#13;&#10;" />
        <footer value="&#13;&#10;======================================================================" />
      <param name="ConversionPattern" value="%d [%t] %-5p - %m%n" />
    </layout>
  </appender>

</log4net>
+2  A: 

Your question isn't exactly clear: are you trying to change the currently-running configuration programmatically, or change the config file programmatically?

If it's the latter, I don't think you need anything from log4net itself - and I wouldn't particularly expect it to provide an API to manipulate the file. Instead, I'd use LINQ to XML (assuming .NET 3.5 or higher) and manipulate it myself directly. It's pretty simple, by the looks of it. Study the configuration file format, work out what changes you'd want to make to it, then write the appropriate code for it. You shouldn't even need to reference log4net for this.

Jon Skeet
Err, Sorry Oh Mr.Skeets!,I was trying to change the change the config file programmatically,so that next time it runs the the debug logging does not happen etc..and to wrap around...Log File detail level : Debugging | Information | Errors | None.So what is your final suggestion based on my new input ?
abmv
@abmv: don't you just have to change the level element?
Jon Skeet
True , but what I was thinking is there any class in log4net to set that (so that it sets it in the config) or I have to use system.xml and do the work myself)
abmv
@abmv: I don't know of anything in log4net for that - and as I put in the answer, I wouldn't really expect there to be. Editing the file should be really easy in LINQ to XML though.
Jon Skeet