2011-10-19 145 views
0

我正在使用企業庫進行日誌記錄。所以,爲了保持我的配置,我使用客戶端的app.config。該要求更改爲「拆分EL配置和UI配置」。我用enterpriseLibrary.ConfigurationSource做了它。將配置拆分爲app.config(對於UI)和EL.config(對於EL)。App.config和企業庫

現在我想從app.cpnfig隱藏對此EL.config的引用,以便僅僅存在此EL> config對用戶是隱藏的。

的App.config代碼:

<enterpriseLibrary.ConfigurationSource selectedSource="EntLib Configuration Source"> 
<sources> 
    <add name="EntLib Configuration Source" type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.FileConfigurationSource, Microsoft.Practices.EnterpriseLibrary.Common, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" 
    filePath="C:\My.CommonServices.Logging\My.CommonServices.Logging\EL.config" /> 
</sources> 

回答

5

您可以使用FileConfigurationSource以編程方式加載外部配置文件。

在應用程序加載或初始化,您可以加載外部配置文件:

FileConfigurationSource fcs = 
    new FileConfigurationSource(
     @"C:\My.CommonServices.Logging\My.CommonServices.Logging\EL.config" 
    ); 

var builder = new ConfigurationSourceBuilder(); 
builder.UpdateConfigurationWithReplace(fcs); 

EnterpriseLibraryContainer.Current = 
    EnterpriseLibraryContainer.CreateDefaultContainer(fcs); 

一旦做到這一點,你可以訪問您最喜愛的功能:

LogWriter logWriter = EnterpriseLibraryContainer.Current.GetInstance<LogWriter>(); 
logWriter.Write("Test"); 

唯一的「絕招」是,確保配置文件總是存在於您期望的位置(絕對或相對)。

+0

謝謝它的工作 – Deimos