简体   繁体   中英

Switch different app.config

I have a windows console application. I get some values from app.config. There are for writing log files. When debug, I want to write them on my desktop. But when publish the application, the logs should be in the remote server. Now I have to manually set the values each time between debugging and publish.

Can we create two version app.config files and use them properly?

When check in, we only check in the app.config for production.

Please remember, it is not an asp.net web application. In that case, we can have different web.config files.

Yes you can, but you need an extension to do it. I have used SlowCheetah for this in the past.

Use SlowCheetah to add a transform for your release build that changes the log-path. Your app.Release.config transform file could look something like this.

<?xml version="1.0" encoding="utf-8" ?>
<!-- For more information on using transformations 
 see the web.comfig examples at http://go.microsoft.com/fwlink/?LinkId=214134. -->
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">

<appSettings>
  <add key="logDirectory" value="D:\releaseLogs" xdt:Transform="Replace" xdt:Locator="Match(key)"/>
</appSettings>

</configuration>

You can add a transform for Test and Debug environments as needed.

In your source control you would check in:

  • An app.config that contains all the keys your application needs
  • An app.Release.config that contains transforms that changes the keys that need to be different in your Release environment.

When your solution is built, SlowCheetah will apply the correct transforms to your app.config based on the target environment.

Another alternative to extensions is to simply add a Post-Build event in your project:

if "Debug"=="$(ConfigurationName)" goto :nocopy
del "$(TargetPath).config"
copy "$(ProjectDir)\App.Release.config" "$(TargetPath).config"
:nocopy

Copy your App.config to App.Release.config and change as needed, when you publish as Release the appropriate file will be used.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM