我有一个使用 app.config 的 WPF 应用程序。我按照Read/Write App.Config File with .NET 2.0中的描述设置了 app.config 文件, 所以它看起来像这样:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="BDViewerLog.txt"/>
<appendToFile value="true"/>
<rollingStyle value="Size"/>
<maxSizeRollBackups value="5"/>
<maximumFileSize value="10MB"/>
<staticLogFileName value="true"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %level %logger - %message%newline"/>
</layout>
</appender>
<startup><supportedRuntime version="v2.0.50727" sku="Client"/></startup><system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="Service1Soap" closeTimeout="00:01:00" openTimeout="00:01:00"
receiveTimeout="00:10:00" sendTimeout="00:01:00" allowCookies="false"
bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
useDefaultWebProxy="true">
<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
maxBytesPerRead="4096" maxNameTableCharCount="16384" />
<security mode="None">
<transport clientCredentialType="None" proxyCredentialType="None"
realm="" />
<message clientCredentialType="UserName" algorithmSuite="Default" />
</security>
</binding>
<binding name="Service1Soap1" closeTimeout="00:01:00" openTimeout="00:01:00"
receiveTimeout="00:10:00" sendTimeout="00:01:00" allowCookies="false"
bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
useDefaultWebProxy="true">
<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
maxBytesPerRead="4096" maxNameTableCharCount="16384" />
<security mode="None">
<transport clientCredentialType="None" proxyCredentialType="None"
realm="" />
<message clientCredentialType="UserName" algorithmSuite="Default" />
</security>
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:9139/Service.asmx" binding="basicHttpBinding"
bindingConfiguration="Service1Soap" contract="ServiceReferenceBDV.Service1Soap"
name="Service1Soap" />
<endpoint address="http://localhost:9139/Service1.asmx" binding="basicHttpBinding"
bindingConfiguration="Service1Soap1" contract="ServiceReference1.Service1Soap"
name="Service1Soap1" />
</client>
</system.serviceModel>
<appSettings>
<add key="Path1" value="C:\files\Accounts.txt"/>
<add key="PathMD5" value="C:\files\md5Accounts.txt"/>
<add key ="LogUserActions" value="C:\files\Log.txt"/>
</appSettings>
</configuration>
在 C# 代码中:
string path = ConfigurationManager.AppSettings["Path1"];
我需要读取 appSettings 节点中文件的路径,但我不断收到错误配置系统无法初始化。我在类似主题中寻找解决方案,但没有一个适用于我的案例。我多次检查文件,找不到错误。我还尝试从\Debug、\Obj 中删除所有文件并清理和重建解决方案,但没有任何效果。我不知道还能做什么。
修复它:我没有在 appender 部分前面添加这个:
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
</configSections>
<log4net>
<appender ...>
</appender>
</log4net>