log4net, whats up with this config

jenson-button-event picture jenson-button-event · Jan 20, 2012 · Viewed 7.1k times · Source

log4net is reporting:

No appenders could be found for logger log4net: Logger: Please initialize the log4net system properly.

I have a stand alone log4net config file:

<!--?xml version="1.0"?-->
<configuration>
  <configsections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net, Version=1.2.10.0, Culture=neutral, PublicKeyToken=1b44e1d426115821" />
  </configsections>
  <log4net>
    <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
      <file value="c:\temp\log.txt" />
      <appendToFile value="true" />
      <rollingStyle value="Size" />
      <maxSizeRollBackups value="10" />
      <maximumFileSize value="5MB" />
      <staticLogFileName value="true" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
      </layout>
    </appender>


  </log4net>
</configuration>

..and this app start code

private static ILog logger;
protected void Application_Start(object sender, EventArgs e)
{
    ////This tells log4net where to go and look for its configuration file and also to watch it for any changes
    log4net.Config.XmlConfigurator.ConfigureAndWatch(new System.IO.FileInfo(@"C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\Config\Moogle\Log4Net.config"));
    logger = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
    logger.Debug("Application_Start Fired");

Answer

gregwhitaker picture gregwhitaker · Jan 20, 2012

You have only defined an appender. You also need to define a root logger and a logger to use the appender (they can be the same logger if you only need one). Add the following root element below to your log4net config.

<root>
  <level value="DEBUG" />
  <appender-ref ref="RollingLogFileAppender" />
</root>