I am trying to make my logger class library to get a few of its config settings from the actual application's App.Config.
Here is my Helper Class from the Logger Library:
public class SettingsHelper
{
private static readonly Configuration Config =
ConfigurationManager.OpenExeConfiguration
(ConfigurationUserLevel.None);
public static void CheckLogSettings()
{
string key;
key = "LoggingEnabled";
if (Config?.AppSettings?.Settings?[key]?.Value == null)
{
Debug.WriteLine("Set Default " + key);
Config.AppSettings.Settings.Add(key, true.ToString());
Config.Save(ConfigurationSaveMode.Modified, false);
}
key = "MaxLogFileSize";
if (Config.AppSettings.Settings[key]?.Value == null)
{
Debug.WriteLine("Set Default " + key);
Config.AppSettings.Settings.Add(key, (2*1024*1024).ToString());
Config.Save(ConfigurationSaveMode.Modified, false);
}
key = "LogFileName";
if (Config.AppSettings.Settings[key]?.Value == null)
{
Debug.WriteLine("Set Default " + key);
Config.AppSettings.Settings.Add(key, "AppLog.txt");
Config.Save(ConfigurationSaveMode.Modified, false);
}
}
public static bool GetLoggingEnabled()
{
return bool.Parse(Config.AppSettings.Settings["LoggingEnabled"].Value);
}
public static long GetMaxLogFileSize()
{
return long.Parse(Config.AppSettings.Settings["MaxLogFileSize"].Value);
}
public static string GetLogFileName()
{
return Config.AppSettings.Settings["LogFileName"].Value;
}
UPDATE
snipit from my App.Config:
<applicationSettings>
<WindowsFormsApplication1.Properties.Settings>
<setting name="LoggingEnabled" serializeAs="String">
<value>True</value>
</setting>
<setting name="MaxLogFileSize" serializeAs="String">
<value>2097152</value>
</setting>
<setting name="LogFileName" serializeAs="String">
<value>AppLog.txt</value>
</setting>
</WindowsFormsApplication1.Properties.Settings>
</applicationSettings>
The solution was to manually add to the App.Config:
<appSettings>
<add key="LoggingEnabled" value="True"/>
<add key="MaxLogFileSize" value="2097152"/>
<add key="LogFileName" value="AppLog.txt"/>
</appSettings>
Then as Tim pointed out: ConfigurationManager.AppSettings["SomeKey"]
will work from your class library.