【问题标题】:Log4Net Error: Failed to find configuration section 'log4net' in the application's .config fileLog4Net 错误:无法在应用程序的 .config 文件中找到配置部分“log4net”
【发布时间】:2017-03-01 16:21:50
【问题描述】:

我目前使用一个类库来存储一个使用 log4net 处理日志记录的类。该类在其他项目中使用。

我已经阅读了很多其他问题和答案,但我还没有能够解决它......

我启用了 log4net 的内部调试,因为它不会写入文件,这是我得到的错误:

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

这些是我的文件: log4net.config

<log4net debug="true">
    <appender name="LogFileAppender" type="log4net.Appender.FileAppender">
        <param name="File" value="Logs/log4net.log" />
        <param name="AppendToFile" value="true" />
        <layout type="log4net.Layout.PatternLayout">
        <param name="Header" value="[Header]\r\n" />
        <param name="Footer" value="[Footer]\r\n" />
        <param name="ConversionPattern" value="%d [%t] %-5p %c %m%n" />
        </layout>
    </appender>

    <root>
        <level value="ALL" />
        <appender-ref ref="LogFileAppender" />
    </root>
</log4net>

App.config

<?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <configSections>
        <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
      </configSections>

      <appSettings>
        <add key="log4net.Internal.Debug" value="true"/>
      </appSettings>

      <parameter>
        <parameterName value="@BlogId" />
        <dbType value="Int32" />
        <layout type="log4net.Layout.RawPropertyLayout">
          <key value="BlogId" />
        </layout>
      </parameter>
    </configuration>

AssemblyInfo.cs(就在最后一行)

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

Logger.cs

public class Logger : ILogger
    {
        private static ILog logger = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        public Logger()
        {
            XmlConfigurator.Configure();
            logger.Info("NEW LOGGER INSTANCE CREATED");
        }
}

更新 我通过让使用我的 Logger.cs 的类为 log4net 提供自己的 .config 文件来修复它。现在 log4net 读取给定的 .config 文件并且它可以工作了。

【问题讨论】:

标签: c# config log4net class-library


【解决方案1】:

您已将 log4net 部分定义在您的 app.config 中:

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

这意味着您应该将 log4net.config 的内容添加到 app.config 文件中。

【讨论】:

    【解决方案2】:

    如果您尝试在 log4net 上实现包装器,其中 log4net 在其自己的类库中,而不在在主可执行文件中在你的应用程序中,在你的包装类的静态构造函数中尝试这样的事情:

    static Logger()
    {
        log4net.Config.XmlConfigurator.Configure(new System.IO.FileInfo
                                                (System.IO.Path.GetDirectoryName
    
        (System.Reflection.Assembly.GetExecutingAssembly().Location)));
    }
    

    静态构造器解决方案确保如果加载类库,配置代码将运行。

    您还需要将 log4net.config 文件 从该类库项目复制到与可执行文件相同的文件夹中。您可以在部署应用程序时手动执行此操作;但是,在使用类似(来自类库项目)调试构建后步骤时自动执行此操作会将配置文件放入正确的文件夹中:

    XCOPY "$(TargetDir)*.config" ".\..\..\..\..\bin\Debug" /S /Y /I
    XCOPY "$(TargetDir)*.config" ".\..\..\..\..\bin\Release" /S /Y /I
    
    // Where those paths point to the main executable /bin folders 
    // and build configuration target dirs
    

    使用这种方法的一个很好的理由是,如果您正在利用 MEF 之类的东西,您可以将您的记录器包装类作为 接口 引入,并且您不需要更多地引用 log4net解决方案中的一个项目。您可以使用自己选择的抽象接口与记录器交互,并使用 log4net 作为 Impl。 (桥梁图案)

    祝你好运!

    【讨论】:

      猜你喜欢
      • 2015-12-30
      • 2015-01-14
      • 1970-01-01
      • 2013-07-02
      • 1970-01-01
      • 2020-03-28
      • 1970-01-01
      • 2015-07-19
      • 1970-01-01
      相关资源
      最近更新 更多