Search code examples
c#databaseerror-handlinglog4netadonetappender

Log4Net error : “Failed to find configuration section log4net” AdoNetAppender


I made a new project only for testing. I am trying to use Log4Net to write into my DB. But I am getting this error.I think something in my config is missing.

log4net:ERROR Failed to find configuration section 'log4net' in the application's .config file. Check your .config file for the <log4net> and <configSections> elements. The configuration section should look like: <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />

AssemblyInfo.cs

[assembly: log4net.Config.XmlConfigurator(ConfigFile = "Logging.config", Watch = true)]

Logging.config

<!--This is the root of your config file-->
<configuration>
  <!-- Level 0 -->
  <!--This specifies what the section name is-->
  <configSections>
    <!-- Level 1 -->
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
    <!-- Level 2 -->
  </configSections>
  <log4net>
    <!-- Level 1 -->
    <appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">
      <bufferSize value="1" />
      <connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
      <connectionString value="Data Source=xxx;initial Catalog=xxx; Integrated Security=True;"/>
      <commandText value="INSERT INTO dbo.Log ([Date],[Thread],[Level],[Logger],[Message],[Exception]) VALUES (@log_date, @thread, @log_level, @logger, @message, @exception)" />
      <parameter>
        <parameterName value="@log_date"/>
        <dbType value="DateTime"/>
        <layout type="log4net.Layout.RawTimeStampLayout"/>
      </parameter>
      <parameter>
        <parameterName value="@thread"/>
        <dbType value="String"/>
        <size value="255"/>
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%thread"/>
        </layout>
      </parameter>
      <parameter>
        <parameterName value="@log_level"/>
        <dbType value="String"/>
        <size value="50"/>
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%level"/>
        </layout>
      </parameter>
      <parameter>
        <parameterName value="@logger"/>
        <dbType value="String"/>
        <size value="255"/>
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%logger"/>
        </layout>
      </parameter>
      <parameter>
        <parameterName value="@message"/>
        <dbType value="String"/>
        <size value="4000"/>
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%message"/>
        </layout>
      </parameter>
      <parameter>
        <parameterName value="@exception"/>
        <dbType value="String"/>
        <size value="2000"/>
        <layout type="log4net.Layout.ExceptionLayout"/>
      </parameter>
    </appender>
    <root>
      <level value="ALL"/>
      <appender-ref ref="AdoNetAppender"/>
    </root>
    <logger>
      <!-- Level 2 -->
      <level />
      <!-- Level 3 -->
      <appender-ref />
      <!-- Level 3 -->
    </logger>
  </log4net>
</configuration>

Form1.cs

    private static readonly log4net.ILog log = log4net.LogManager.GetLogger (System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
    private void simpleButton1_Click(object sender, EventArgs e)
    {
        log4net.Config.XmlConfigurator.Configure();
        log.Info("Info logging");
        log.Debug("DADASDASD");
    }

Solution

  • Your log4net conf should be as following you should not put anything on the top of log4net node since your configuration is in an independent file:

    <log4net>
        <!-- Level 1 -->
        <appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">
          <bufferSize value="1" />
          <connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
          <connectionString value="Data Source=xxx;initial Catalog=xxx; Integrated Security=True;"/>
          <commandText value="INSERT INTO dbo.Log ([Date],[Thread],[Level],[Logger],[Message],[Exception]) VALUES (@log_date, @thread, @log_level, @logger, @message, @exception)" />
          <parameter>
            <parameterName value="@log_date"/>
            <dbType value="DateTime"/>
            <layout type="log4net.Layout.RawTimeStampLayout"/>
          </parameter>
          <parameter>
            <parameterName value="@thread"/>
            <dbType value="String"/>
            <size value="255"/>
            <layout type="log4net.Layout.PatternLayout">
              <conversionPattern value="%thread"/>
            </layout>
          </parameter>
          <parameter>
            <parameterName value="@log_level"/>
            <dbType value="String"/>
            <size value="50"/>
            <layout type="log4net.Layout.PatternLayout">
              <conversionPattern value="%level"/>
            </layout>
          </parameter>
          <parameter>
            <parameterName value="@logger"/>
            <dbType value="String"/>
            <size value="255"/>
            <layout type="log4net.Layout.PatternLayout">
              <conversionPattern value="%logger"/>
            </layout>
          </parameter>
          <parameter>
            <parameterName value="@message"/>
            <dbType value="String"/>
            <size value="4000"/>
            <layout type="log4net.Layout.PatternLayout">
              <conversionPattern value="%message"/>
            </layout>
          </parameter>
          <parameter>
            <parameterName value="@exception"/>
            <dbType value="String"/>
            <size value="2000"/>
            <layout type="log4net.Layout.ExceptionLayout"/>
          </parameter>
        </appender>
        <root>
          <level value="ALL"/>
          <appender-ref ref="AdoNetAppender"/>
        </root>
        <logger>
          <!-- Level 2 -->
          <level />
          <!-- Level 3 -->
          <appender-ref />
          <!-- Level 3 -->
        </logger>
      </log4net>
    

    If you are still facing problems try to load the conf as following:

    XmlConfigurator.Configure(new FileInfo("PATH\Logging.config")