log4net 在应用程序的 .config 文件中找不到配置部分“log4net”

问题描述 投票:0回答:1

使用 Apache 的 log4net 并遇到一些有趣的问题。

这是我的log4net.config,值得注意的是我在同一个解决方案中有多个项目,所以这个放在一个共享项目中,

Build Action
“嵌入式资源”和
Copy to Output Directory
“始终复制”(并且添加了到构建后的输出目录,对于我的解决方案中的两个项目)

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

  <log4net>
    <root>
      <level value="INFO" />
      <appender-ref ref="DebugAppender" />
      <appender-ref ref="RollingLogFileAppender" />
      <appender-ref ref="TextBoxAppender" />
    </root>
    <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
      <file value="Logs\app.log" />
      <appendToFile value="true" />
      <rollingStyle value="Composite" />
      <maxSizeRollBackups value="5" />
      <maximumFileSize value="10MB" />
      <staticLogFileName value="true" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date Thread: [%thread] %level %logger - %message%newline" />
      </layout>
    </appender>

    <appender name="DebugAppender" type="log4net.Appender.DebugAppender">
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date{HH:mm:ss.ffff} Thread: [%thread] %level %logger%exception - %message%newline" />
      </layout>
    </appender>

    <appender name="TextBoxAppender" type="app.TextBoxAppender, app">
      <targetFormName value="Form1" />
      <targetControlName value="tbLog" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date{HH:mm:ss.ffff} Thread: [%thread] %level %logger%exception - %message%newline" />
      </layout>
    </appender>
  </log4net>
</configuration>

这是我的 AssemblyInfo.cs

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

我的理解是我可以使用

log4net.Config.XmlConfigurator.Configure()
来验证配置是否正常工作,当我这样做时我得到这个错误

{log4net:ERROR XmlConfigurator: 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" />}

我认为这一切的重点

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

我不需要将它放在我的应用程序 .config 文件中吗?

有趣的是,如果我运行这个

log4net.Config.XmlConfigurator.Configure(new FileInfo("log4net.config"));
它可以正常工作并且完美记录。 有没有办法可以更改我的配置,这样我就不需要添加上面的行了?

logging log4net
1个回答
0
投票

log4net.Config.XmlConfigurator.Configure()
的调用导致错误,因为它指示 log4net 在主应用程序配置文件中查找
log4net
部分,例如
YourApp.exe.config
web.config
.

来自文档

每个应用程序都有一个配置文件。这与附加了“

.config
”的应用程序同名。这个文件是
XML
,调用这个函数会提示配置器在该文件中查找一个名为
log4net
的部分,其中包含配置数据。

您有一个名为

log4net.config
的自定义文件。因此
XmlConfigurator.Configure(new FileInfo("log4net.config"))
成功。


关于您的

log4net.config
文件。

根 xml 标签应该是

<log4net>
而不是
<configuration>
。最后一个只有在使用主应用程序配置文件时才会存在。

<configSections>
声明仅在使用主应用程序配置文件时适用,在使用自定义文件时不适用。

自定义文件示例:

<log4net>
    <root>
      <level value="INFO" />
      <!- more configuration settings here --> 
    </root>  
    <!- more configuration settings here --> 
</log4net>

参见文档

无需将

Build Action
log4net.config
设置为
Embedded resource
,因为无论如何您都会将其复制到输出文件夹。


不要将

[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config")]
属性和对
log4net.Config.XmlConfigurator.Configure(...)
的调用结合起来。

你选一个。

在您的情况下,只需在主应用程序中定义属性就足够了。

[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config")]
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.