我在 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/