Using Log4Net with NOF

Dec 20, 2012 at 3:50 PM

We have added the following Log4Net configuration settings to the Web.config:

 

<log4net>

    <appender name="Output" type="log4net.Appender.DebugAppender">

      <layout type="log4net.Layout.PatternLayout">

        <conversionPattern value="%-4timestamp [%thread] %-5level %logger %ndc - %message%newline" />

      </layout>

    </appender>

    <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">

      <lockingModel type="log4net.Appender.FileAppender+MinimalLock"/>

      <file value="App_Data\Logs\" />

      <datePattern value="dd.MM.yyyy'.log'" />

      <staticLogFileName value="false" />

      <appendToFile value="true" />

      <rollingStyle value="Composite" />

      <maxSizeRollBackups value="10" />

      <maximumFileSize value="5MB" />

      <layout type="log4net.Layout.PatternLayout">

        <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />

      </layout>

    </appender>

    <root>

      <level value="ERROR" />

      <appender-ref ref="RollingLogFileAppender" />

    </root>

  </log4net>

 

When an exception is thrown by the application, we would expect to see details written to the log file but this is not the case. If we use “INFO” instead of “WARN” or “ERROR” then the log file is written to, so the configuration settings are valid and working as expected.

 

In addition to this, the Application_Error() method in Global.asax is not visited when an exception is thrown so we are unable to log the exception here.

 

Is there a way of logging Exceptions thrown by the application?

Coordinator
Dec 20, 2012 at 4:30 PM

Exceptions neither get logged by default nor necessarily get logged to 'ERROR'. Eg some exceptions are expected results from an API some are simply not appropriately logged as errors.  So no you can't just log all exceptions. Of course if you're developing you can break on exceptions in the debugger.

Application_Error() is only going to get hit if the exception is unhandled within the framework. Most exceptions should get handled and directed to the appropriate page. Eg see OnException in GenericControllerImpl.cs assuming it wasn't handled earlier. Otherwise you should see the 'Yellow Screen of Death'.

If there's a particular case causing a problem please flag it and we can improve logging and or diagnostics around it.