简体   繁体   English

应用程序在修改Log4net的App.config时崩溃

[英]Application crashes at modifying my App.config for Log4net

This is my first time ever using Log4Net so I'm sure I'm doing something really dumb. 这是我第一次使用Log4Net,所以我确定我做的事情真是愚蠢。

In App.config , inside the <configuration> tag, I have the following: App.config ,在<configuration>标记内,我有以下内容:

<log4net>
   <appender name="LogFileAppender" type="log4net.Appender.FileAppender">
     <param name="File" value="log-file.txt" />
     <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 [%x] &lt;%X{auth}&gt; - %m%n" />
     </layout>
   </appender>

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

In my Program.cs file I catch a ConfigurationException: 在我的Program.cs文件中,我发现了一个ConfigurationException:

Message: Configuration system failed to initialize
InnerException: Unrecognized configuration section log4net.

But I copy-pasted the configuration code straight out of the .NET example they provide on their website. 但我直接从他们网站上提供的.NET示例中复制粘贴配置代码。 I read over their documentation but it's pretty confusing for someone who has never used any sort of logging framework before. 我阅读了他们的文档,但对于从未使用过任何类型的日志框架的人来说,这非常令人困惑。 I also found a few tutorials on CodeProject that hold your hand better but can't piece out what I've done wrong. 我还找到了一些关于CodeProject的教程,这些教程可以让你的手更好,但不能弄明白我做错了什么。 I tried putting the <root> element before the <appender> element and that gives me the same ConfigException. 我尝试将<root>元素放在<appender>元素之前,这给了我相同的ConfigException。 And yes I've added a reference to the DLL to the project. 是的,我已经将DLL的引用添加到项目中。

Sounds like you just forgot to include the configSection entry for log4net : 听起来你只是忘了包含log4netconfigSection条目:

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

    <!-- other configurations, such as appSettings and whatnot -->

    <log4net>
        <!-- log4net configuration that you already have -->
    </log4net>
</configuration>

In order to use the log4net config node the system needs to be told how to interpret that node, which is handled in the log4net.Config.Log4NetConfigurationSectionHandler class. 为了使用log4net配置节点,需要告知系统如何解释该节点,该节点在log4net.Config.Log4NetConfigurationSectionHandler类中处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM