Deimos
Deimos

Reputation: 3

App.config and Enterprise library

I'm using enterprise library for logging. So, to hold my configurations, I'm using the client's app.config. The requirement changed to "split EL configuration and UI configuration". I did it using enterpriseLibrary.ConfigurationSource. Split the configurations to app.config(For UI) and EL.config(For EL).

Now I want to hide the reference to this EL.config from app.cpnfig, so that the mere existence of this EL>config is hidden from the User.

App.config Code:

 <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>

Upvotes: 0

Views: 3643

Answers (1)

Randy Levy
Randy Levy

Reputation: 22655

You can use FileConfigurationSource to programmatically load an external configuration file.

During application load or initialization you can load your external configuration file:

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);

Once that is done you can access your favorite features:

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

The only "trick" is ensuring that the configuration file is always present where you expect (either absolute or relative).

Upvotes: 5

Related Questions