添加log4net配置时发生异常 [英] Exception when adding log4net config
问题描述
我在App.cs
文件(正在创建一个只读变量)的第一行代码中遇到错误.我收到的错误是:
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:
"System.TypeInitializationException"类型的第一次机会异常 发生在PresentationFramework.dll中,类型的未处理异常 发生"System.TypeInitializationException" PresentationFramework.dll附加信息:类型初始化器 'System.Windows.Application'引发了异常.
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中看到的消息弹出窗口:
This is the message popup I get in VS:
类型为'System.TypeInitializationException'的未处理异常 在PresentationFramework.dll中发生
An unhandled exception of type 'System.TypeInitializationException' occurred in PresentationFramework.dll
其他信息:的类型初始值设定项 "System.Windows.Application"抛出异常.
Additional information: The type initializer for 'System.Windows.Application' threw an exception.
我所做的唯一更改就是将其添加到我的app.config
中:
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>
推荐答案
问题是我在文件app.config
的开头而不是结尾处有<startup>
xml节点.它必须是app.config
文件中的最后一件事.
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>
这篇关于添加log4net配置时发生异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!