Unrecognized configuration section log4net

C#asp.netLog4net

C# Problem Overview


I have this code in web.config:

<log4net>
  <root>
    <level value="ALL" />
    <appender-ref ref="LogFileAppender" />
  </root>
  <appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender">
    <param name="File" value="D:\logFileFaculty.txt" />
    <param name="AppendToFile" value="true" />
    <rollingStyle value="Size" />
    <maxSizeRollBackups value="10" />
    <maximumFileSize value="10MB" />
    <staticLogFileName value="true" />
    <layout type="log4net.Layout.PatternLayout">
      <param name="ConversionPattern" value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
    </layout>
  </appender>
</log4net>

And I have downloaded log4net.dll and placed it in Bin Folder.

In one of My aspx.cs pages I have added this code:

using log4net;
[assembly: log4net.Config.XmlConfigurator(Watch = true)]


private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

But it's giving error as Unrecognized configuration section log4net.

C# Solutions


Solution 1 - C#

You need to declare the log4net section:

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

Take a closer look at the documentation which explains the necessary things to do.

Solution 2 - C#

<configuration>
    <configSections>
        <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,Log4net"/>
    </configSections>
</configuration>

add to your app.config file

Solution 3 - C#

Not sure why not post the whole example.

<configuration>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
  </configSections>
  ...
  <log4net>
    ...
  </log4net>
</configuration>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionShreyas AcharView Question on Stackoverflow
Solution 1 - C#Darin DimitrovView Answer on Stackoverflow
Solution 2 - C#lambexView Answer on Stackoverflow
Solution 3 - C#YolaView Answer on Stackoverflow