简体   繁体   中英

How to read log4Net from web.config

I am using log4net to perform daily logging.
I realize that log4net doesn't support deleting old files in this fashion.

I am trying to write my own method to accomplish this task, however I am no sure how to read the log4net settings from the web.config file.

I've tried:

var log4NetData = ConfigurationManager.GetSection("log4net");

However I get this as my results:

The type 'System.Configuration.ConfigXmlElement' exists in both 'System.Configuration.dll' and 'System.dll'

How can I read the log4net node from my web.config?

Actually Log4Net can update the same file always (this way, there is no need to delete it and create another one). I'm using log4Net to log always the same file n times in a hour. My configurations:

private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

app.config:

<configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
  </configSections>  
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
  </startup>
  <log4net>
    <root>
      <level value="ALL" />
      <appender-ref ref="MyFileAppender" />
    </root>
    <appender name="MyFileAppender" type="log4net.Appender.FileAppender">
      <file value="application.log" />
      <appendToFile value="true" />
      <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date %level - %message%newline" />
      </layout>
    </appender>
  </log4net>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM