Search code examples
c#asp.net-web-apilog4netowin-middleware

CORS issue when adding log4net config


Context:

I have a simple Web API project with a controller and an action. In the header of each request, I add an authorization token. Everything work as expected. I can request a token based on some credentials, and use it to make HTTP requests successfully.

Problem: When I add the following in the web.config:

<log4net>
<appender name="FileAppender" type="log4net.Appender.FileAppender">
  <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
  <file value="logfile.txt" />
  <appendToFile value="true" />
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%date: %-5level – %message%newline" />
  </layout>
</appender>
<root>
  <level value="DEBUG" />
  <appender-ref ref="FileAppender" />
</root>
</log4net>

I get the following error:

XMLHttpRequest cannot load http://localhost:4042/token. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8080' is therefore not allowed access. The response had HTTP status code 500.

If I remove the configuration for log4net everything works as expected.

Note: I do not have any setting in web.config that adds headers in a request.

Startup.cs is the only point in the project were I specify:

app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);

Question: How does the log4net configuration relate to preflight requests? (based on my limited knowledge about the ASP.NET framework I would say it is non-sense). Is there a subtlety that am I missing?


Solution

  • I looked into the webconfig of my project and there is a part from a working web.config

       <configSections> 
             <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" /> 
           </configSections> 
           <log4net> 
             <appender name="FileAppender" type="log4net.Appender.FileAppender"> 
              ...
            </log4net> 
    

    Maybe you missed configSection?