2

I have created a Visual Studio extension representing a Visual Studio Project wizard (vsix package). I am attempting to wire up log4net, and this has been unsuccessful. I have chased the issue down to my app.config not being loaded properly.

I have added this to my app.config in my visual studio extension:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <appSettings>
    <add key="test" value="hello world"/>
  </appSettings>
</configuration>

and within my IWizard implementation, I have added this line of code:

var test = System.Configuration.ConfigurationManager.AppSettings["test"];

However, the test variable above is always null when debugging.

I have verified these things:

  • App.config is set to Build Action: Content
  • App.config is set to Copy to Output Directory: Copy Always
  • App.config is set to Include in VSIX: True
  • App.config file is present in my C:\Users\<user>\AppData\Local\Microsoft\VisualStudio\16.0_...\Extensions\<Name>\<Company>\1.0 folder

What am I missing? And if App.config is not allowed within VSIX extension development, how would you go about wiring up log4net?

jdylanmc
  • 829
  • 11
  • 24
  • Please check if [this document](https://stackoverflow.com/questions/505566/loading-custom-configuration-files) could help you. – Mr Qian Dec 10 '19 at 07:47

1 Answers1

5

What am I missing? And if App.config is not allowed within VSIX extension development, how would you go about wiring up log4net?

An App.Config file is copied and renamed during build to .exe.config, so only executables can have and use "App.Config" files using ConfigurationManager directly. Usually,a VSIX project generates a DLL that is loaded in the Visual Studio executable (devenv.exe), so your project, using ConfigurationManager directly, can only read settings from the devenv.exe.config (folder C:\Program Files (x86)\Microsoft Visual Studio 16.0\Common7\IDE).

And when l test it with my only app.config file in a vsix project,the project seems to be unable to get the value of my custom files only from the default devenv.exe.config which contains only two values TestProjectRetargetTo35Allowed and EnableWindowsFormsHighDpiAutoResizing.This means that in any case, it gets values from devenv.exe.config.

Solution

1#. you can just define the new key in the devenv.exe.config file and you can get it directly from the file.

 <appSettings>
    <add key ="TestProjectRetargetTo35Allowed" value ="true"/>
    <add key ="EnableWindowsFormsHighDpiAutoResizing" value ="true"/>
      insert new key here
  </appSettings>

2#. You can get this app.config by code and get the values of the keys directly from it.

ExeConfigurationFileMap configMap = new ExeConfigurationFileMap();
configMap.ExeConfigFilename = @"xxxxxxxx"; // the path of the custom app.config
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(configMap, ConfigurationUserLevel.None);
var test = config.AppSettings.Settings["test"].Value;

In addition, I recommend solution2 is better and easier to solve your issue. Hope it could help you.

Mr Qian
  • 21,064
  • 1
  • 31
  • 41
  • Although I am not happy with the answer (as I will have to somehow inject the configuration to other libraries) - this is the correct one. – Pavel Donchev Feb 13 '21 at 03:21
  • var appConfigPath = System.IO.Path.GetDirectoryName(Package.GetType().Assembly.Location); // add in your app config whatever it's name var fullyQualifiedAppConfig = System.IO.Path.Combine(configPath, "App.config"); – AUSTX_RJL Jul 07 '22 at 16:12