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

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

我新建了一个项目,只是为了测试。我正在尝试使用 Log4Net 写入我的数据库。但是我得到这个 error.I 认为我的配置中缺少某些东西。

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");
    }

你的 log4net conf 应该如下你不应该把任何东西放在 log4net 节点的顶部,因为你的配置是在一个独立的文件中:

<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>

如果您仍然遇到问题,请尝试按以下方式加载 conf:

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