c# - 添加 log4net 配置时出现异常

标签 c# log4net log4net-configuration

我在 App.cs 文件(创建只读变量)的第一行代码中遇到错误。我得到的错误是:

A first chance exception of type 'System.TypeInitializationException' occurred in PresentationFramework.dll An unhandled exception of type 'System.TypeInitializationException' occurred in PresentationFramework.dll Additional information: The type initializer for 'System.Windows.Application' threw an exception.

这是我在 VS 中得到的消息弹出窗口:

An unhandled exception of type 'System.TypeInitializationException' occurred in PresentationFramework.dll

Additional information: The type initializer for 'System.Windows.Application' threw an exception.

我所做的唯一更改是将其添加到我的 app.config 中:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <startup> 
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.1"/>
  </startup>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
  </configSections>
  <log4net>
    <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender" >
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level %logger [%ndc] - %message%newline" />
      </layout>
    </appender>
    <root>
      <level value="INFO" />
      <appender-ref ref="ConsoleAppender" />
    </root>
  </log4net>
</configuration>

最佳答案

问题是我有 <startup> app.config 中的 xml 节点文件开头而不是文件末尾。它必须是 app.config 中的最后一件事文件。

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  // lots of other stuff here...
  <startup> 
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.1"/>
  </startup>
</configuration>

关于c# - 添加 log4net 配置时出现异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22596406/

相关文章:

c# - 如何在任何给定时间引发事件

c# - 安装 log4net 多个项目

log4net 与 Nlog

azure - AppServiceAppLogs 中未显示 log4net 项目

c# - 如何通过代码启用/禁用存储在配置中的日志记录和文件附加信息?

c# - 从外部应用程序向 Web 浏览器添加书签

c# - MVC区域路由解析

logging - Log4Net 多个 Appender,多个表存在配置问题

c# - PayPal 的特定 log4net 日志文件

c# - 在 Entity Framework Core 的系统版本时态表中查询数据