Log4Net错误:“找不到配置部分log4net”

时间:2014-11-17 15:21:23

标签: c# log4net app-config

我开始使用log4net。我正在开发一个小型控制台项目,我必须在其中实现此框架。

一开始我创建了一个小型控制台项目,看看它是如何工作的,没有任何其他代码。 我设法让它正常工作。

现在我尝试迁移我的应用程序中的所有代码,当我执行我的控制台应用程序的.exe时出现此错误:

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

代码:

public class Program
{
    private static readonly log4net.ILog log = log4net.LogManager.GetLogger
    (System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
    public static void Main(string[] args)
    {
        log4net.Config.XmlConfigurator.Configure();
        ILog log = log4net.LogManager.GetLogger(typeof(Program));
        ...

app.config尚不存在,因此其内容与我所做的测试项目相同:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,Log4net" />
    </configSections>
  <log4net>
    <appender name="FileAppender" type="log4net.Appender.FileAppender,log4net">
      <file value="D:\WEB\SAI\log\nas\log.txt" />
      <appendToFile value="true" />
      <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %level %logger - %message%newline" />
      </layout>
      <filter type="log4net.Filter.LevelRangeFilter">
        <levelMin value="INFO" />
        <levelMax value="FATAL" />
      </filter>
    </appender>

    <root>
      <level value="INFO"/>
      <appender-ref ref="FileAppender"/>
    </root>
  </log4net>
</configuration>

在互联网上进行一些搜索后,我补充道:

[assembly: log4net.Config.XmlConfigurator()]

到我的AssemblyInfo.cs,因为它被建议但结果仍然相同... 在此先感谢您的帮助

1 个答案:

答案 0 :(得分:0)

添加 [程序集:log4net.Config.XmlConfigurator(ConfigFile =“ log4net.config”,监视= true)] 在AsseblyInfo.cs

相关问题