views:

292

answers:

3

Hi,

I'm currently trying to create the app.config at runtime. The basic idea is that I'm deploying several config files and based on an environment variable the application itself decides which one to use.

Currently I'm copying the config file I'd like to use to myapp.exe.config, and refresh all the sections. The problem I'm facing is, that log4net seems to mess up things, but only in release build (as the static fields are initilized at a different time).

I explicitly DON'T want to decide at build time which config to use.

Any ideas (probably there is a better approach)?

tia Martin

+1  A: 

You can configure log4net after you decided what config to use and copied it into place by using XmlConfigurator.Configure(new System.IO.FileInfo("Filename.config"))

Alex Reitbort
There is even a `XmlConfigurator.ConfigureAndWatch` method in log4net which reloads the config whenever changes are made.
Scoregraphic
A: 

Can you do all the copying in one AppDomain when you start up - an AppDomain which doesn't use any settings - and then start the real application in a new AppDomain? Heck, as an alternative would it be possible to have separate processes for this? One bootstrap process would just make sure the right configuration is in place, then start up the real application.

In both of these solutions you would do everything you need before you really use any settings, which should keep things simpler.

Note that static initialization can be controlled to some extent by the presence or absence of a static constructor - see my article on beforefieldinit for more details. I wouldn't recommend using this to fix your current system though - it'll end up being quite fragile when it comes to maintaining the code.

Jon Skeet
Hi, thx for the hint. I know that adding static ctor's would solve my problem, but I would like to avoid this, as you said -> maintaining such code is not fun.
Martin Moser
A: 

Instead of config files, couldn't you use Application Settings? If it's not something the user needs to change, you might be better off coming up with several sets of internally scoped Settings and just switching between them.

womp
no because I'm using external components, which are expecting some custom sections.
Martin Moser
Ah too bad. . . .
womp