Exception when adding log4net config

送分小仙女□ 提交于 2019-12-18 04:41:59

问题


I am getting an error on the very first line of code in the App.cs file (which is creating a readonly variable). The error I am getting is:

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.

This is the message popup I get in VS:

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

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

The only change I have made is adding this to my 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>

回答1:


The issue was that I had the <startup> xml node in the app.config file at the start rather than the end of the file. It needs to be the last thing in the app.config file.

<?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>


来源:https://stackoverflow.com/questions/22596406/exception-when-adding-log4net-config

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!