在DLL Project中定义配置文件

前几日,新建了一个访问数据库的专用DLLProject,在其中添加EntityFramework等参照。在VS的开发环境下,相关参照及配置文件会自动生成,并反应在App.Config配置文件夹中。

 现在问题很明显,当再次新建一个Console,或者WindowsForm (带Main静态方法) Project时,参照前面定义的DLL,其配置文件将不会自动加载到Main方法对应的配置文件中。

解决方法: 

将DLL Project中App.Config文件,新增的部分,全部拷贝到 Main Project中的App.Config。 

下面的配置文件包括了,引用MySQL的数据库,Log4net等DLL。如下:

 

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
  </configSections>
  <connectionStrings>
    <add name="nilisEntities" connectionString="metadata=res://*/DataEntity.DataModel.csdl|res://*/DataEntity.DataModel.ssdl|res://*/DataEntity.DataModel.msl;provider=MySql.Data.MySqlClient;provider connection string=&quot;server=localhost;user id=sa;password=jbd12345;persistsecurityinfo=True;database=nilis&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
  </entityFramework>
  <log4net debug="true">
    <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
      <file type="log4net.Util.PatternString" value="C:Logs.log" />
      <appendToFile value="true" />
      <rollingStyle value="Date" />
      <preserveLogFileNameExtension value="true" />
      <staticLogFileName value="false" />
      <datePattern value="'NIS_Process_'yyyyMMdd" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level - %message%newline" />
      </layout>
    </appender>
    <root>
      <level value="ALL" />
      <appender-ref ref="RollingLogFileAppender" />
    </root>
  </log4net>
</configuration>
Love it, and you live without it
原文地址:https://www.cnblogs.com/tomclock/p/7403095.html