Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Configuration section 'log4net' is missing a section declaration

I'm struggling adding log4net to my MVC5 project. I have done the following;

Install-Package log4net

which has successfully installed (I assume) log4net

I have added the following to my web.config inside the cofiguration section;

<log4net debug="true">
    <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
        <file value="Logs\ApiLog.txt" />
        <appendToFile value="true" />
        <rollingStyle value="Size" />
        <maxSizeRollBackups value="10" />
        <maximumFileSize value="10MB" />
        <staticLogFileName value="true" />
        <layout type="log4net.Layout.PatternLayout">
           <conversionPattern value="%-5p %d %5rms %-22.22c{1} %-18.18M - %m%n" />
        </layout>
    </appender>

    <root>
        <level value="DEBUG" />
        <appender-ref ref="RollingLogFileAppender" />
    </root>
</log4net>

and I have the following added to my configSections in the web.config;

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

I have added the following to my Global.asax.cs;

log4net.Config.XmlConfigurator.Configure();

The solution compiles, However when I try to run my program I get the error;

HTTP Error 500.19 - Internal Server Error

The requested page cannot be accessed because the related configuration data for the page is invalid.

The configuration section 'log4net' cannot be read because it is missing a section declaration

Does anyone have anyideas what I am doing wrong please?

like image 620
Matthew Flynn Avatar asked Oct 08 '15 11:10

Matthew Flynn


2 Answers

You need to include this as well in web.config

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

it should look like below, Also make sure you have log4net.dll, log4net.xml in your Application bin folder

<configuration>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
  </configSections>
  <appSettings>    
  </appSettings>
  <log4net xsi:noNamespaceSchemaLocation="http://csharptest.net/downloads/schema/log4net.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
      <file value="logfile.log" />
      <appendToFile value="true" />
      <rollingStyle value="Date" />
      <datePattern value=".yyyyMMdd.lo\g" />
      <maximumFileSize value="5MB" />
      <maxSizeRollBackups value="-1" />
      <countDirection value="1" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date %-5level [%thread] [%aspnet-session{SessionId}] %logger - %message%newline%exception" />
      </layout>
    </appender>
    <root>
      <level value="DEBUG" />
      <appender-ref ref="RollingLogFileAppender" />
    </root>
  </log4net>
like image 65
Rolwin Crasta Avatar answered Sep 30 '22 01:09

Rolwin Crasta


You're probably missing to declare the config section. Try this in your web.config:

<configuration>
  <configSections>
    <section name="log4net" requirePermission="false" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
  <configSections>
<configuration>
like image 35
kay.herzam Avatar answered Sep 30 '22 01:09

kay.herzam