Search code examples
c#app-configclass-library

How to save config changes back to App.Config


I am trying to make my logger class library to get a few of its config settings from the actual application's App.Config.

  1. Nothing is actually saved. Every time I run the application it sets the default values. But I still can reference them during the run.
  2. If it is saving the settings to a file I can't find the file. Its not app.config or user.config in the bin/Debug/ directories.
  3. I run CheckLogSettings every time the program starts.

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>

Solution

  • 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.