I have a console app project that write some info to a common.app.config file which other library project will read from in runtime. The writing operation is as follows:
var configMap = new ExeConfigurationFileMap
{
ExeConfigFilename = @"C:\CONSOLE_APP_PROJECT_PATH\common.app.config"
};
var config = ConfigurationManager.OpenMappedExeConfiguration(configMap, ConfigurationUserLevel.None);
config.AppSettings.Settings.Add("FullLogPath",combinedPath);
config.Save(ConfigurationSaveMode.Minimal,true);
ConfigurationManager.RefreshSection("appSettings");
Thw writing operation is done. To access this common.app.config file, I tried the procedures in this link :
Reuse config values across projects
I have added the common.app.config file from the other library project as link to avoid copying. So whatever that is written in runtime will be reflected in the linked common.app.config file .After linking from other project , the .cproj file has the following update:
<Content Include="..\..\DiagnosticsServiceHost\common.app.config">
<Link>common.app.config</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
The project that refers the common config has its own configuration file (App.config) and inside it I have made the following changes:
<appSettings file=".\bin\x86\Debug\common.app.config">
</appSettings>
But I cannot access the value of the key - FullLogPath in runtime from common.app.config with following snippet: (the value of FullLogPath is null)
public class AppSettingsParent
{
protected AppSettingsParent()
{
}
// Only meant to be used in testing environments
private static AppSettingsSection AppSettingsSection { get; } = null;
protected static string Get([CallerMemberName] string propertyName = null)
{
if (AppSettingsSection != null)
{
return AppSettingsSection.Settings[propertyName].Value;
}
return ConfigurationManager.AppSettings[propertyName];
}
}
public class CommonAppSettings : AppSettingsParent
{
private CommonAppSettings()
{
}
public static string FullLogPath => Get();
}
private static string InitLogFile(string ecu, string filename)
{
var folder = Path.Combine(CommonAppSettings.FullLogPath + alu + "\\");
var absoluteFilePath = Path.Combine(folder.Trim(), filename.Trim() + "-" + alu.Trim() + ".log");
return absoluteFilePath;
}