title: Using log4net to log unhandled errors when using OWIN
body: <p>Can you point me to some log4net tutorial or samples that logs all unhandled exceptions occurring on my MVC Web Api when using OWIN?</p>
  
  <p>I have a <code>Startup</code> class that configures <code>Castle Windsor</code> and <code>OAuth</code> and I'm not sure how application errors are handled for OWIN enabled applications.  </p>
  
  <p>I have tried putting the log4net code in the <code>Application_Error</code> event in <code>global.asax</code> but it never gets executed. </p>
  
  <pre><code>public class WebApiApplication : System.Web.HttpApplication {
      protected void Application_Start() {
          log4net.Config.XmlConfigurator.Configure();
      }        
      protected void Application_Error(object sender,EventArgs e) {
          Exception lastException = Server.GetLastError();
          var logger = log4net.LogManager.GetLogger(typeof(WebApiApplication));
          logger.Fatal(lastException);
      }
  }
  </code></pre>
  
  <p>Any ideas why this is?</p>
  
  <p><strong>EDIT: web.config</strong> with log4net configuration:</p>
  
  <pre><code>&lt;configSections&gt;
      &lt;section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" /&gt;
  &lt;/configSections&gt;
  
  &lt;log4net debug="true"&gt;
      &lt;logger name="SiteWorksAPI"&gt;
        &lt;level value="Debug" /&gt;
        &lt;appender-ref ref="EventLogAppender" /&gt;
      &lt;/logger&gt;
      &lt;appender name="FileAppender" type="log4net.Appender.FileAppender"&gt;
        &lt;file value="C:\ProgramData\Site\SiteWorksAPILog.txt" /&gt;
        &lt;appendToFile value="true" /&gt;
        &lt;lockingModel type="log4net.Appender.FileAppender+MinimalLock" /&gt;
        &lt;immediateFlush value="true" /&gt;
        &lt;layout type="log4net.Layout.PatternLayout"&gt;
          &lt;conversionPattern value="============================================================%nLogger = %logger.%nLocation = %location.%nUnique Token = %property{x-fcc-uniquetoken}.%nDate = %date{dd MMM yyyy - HH:mm:ss}.%nMessage = %message%n" /&gt;
        &lt;/layout&gt;
      &lt;/appender&gt;
      &lt;appender name="EventLogAppender" type="log4net.Appender.EventLogAppender"&gt;
        &lt;applicationName value="SiteWorks.API" /&gt;
        &lt;logname value="SiteWorks API Log" /&gt;
        &lt;layout type="log4net.Layout.PatternLayout"&gt;
          &lt;conversionPattern value="Date: %date{dd MMM yyyy - HH:mm:ss} %nMessage: %message %nIdentity: %identity %nUnique Token: %property{x-fcc-uniquetoken} %nChange Time Stamp: %property{x-fcc-changedatetime} %nOriginal Time Stamp: %property{x-fcc-originaltimestamp} %nOriginating System: %property{x-fcc-originatingsystem} %nOriginating User: %property{x-fcc-originatingusername} %nRequest Uri: %property{Request} %nResponse Headers: %property{ResponseHeaders}%nLogger: %logger  %nThread: %thread  %n%n%exception" /&gt;
        &lt;/layout&gt;
      &lt;/appender&gt;
  &lt;/log4net&gt;
  </code></pre>