1

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?

sideshowbarker
  • 81,827
  • 26
  • 193
  • 197
Radu Cojocari
  • 1,759
  • 1
  • 22
  • 25

2 Answers2

1

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?

aniski
  • 1,263
  • 1
  • 16
  • 31
0

use an external setting file for avoiding the problem :

At startup, call:

XmlConfigurator.Configure();

In your Web.config, specify log4net.Config in appSettings:

<add key="log4net.Config" value="Log.config" />

In the Log.config file put your configuration :

<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>
Dypso
  • 563
  • 1
  • 5
  • 15