.net default app config file name change

1 message Options
Embed this post
Permalink
ddcows

.net default app config file name change

Reply Threaded More More options
Print post
Permalink
(This post was updated on )
Hi,

When we upgraded to visual studio 2008, the default application configuration file name changed from app.exe.config to app.config.  The change only manifests when the application either doesn't use the configuration file at all or creates it dynamically by calling ConfigurationManager.OpenExeConfiguration.

Nevertheless, when NLog's LogFactory.Configuration getter calls AppDomain.CurrentDomain.SetupInformation.ConfigurationFile, it returns app.config instead of app.exe.config.

Since our existing .nlog files are named app.exe.nlog, they are no longer found by NLog.

Has anyone else encountered this problem?

If so, here is some code I added that makes NLog more flexible.  Regardless of whether the config file is named app.exe.config or app.config, NLog will find a .nlog file named either app.nlog or app.exe.nlog.

        public LoggingConfiguration Configuration
        {
            get
            {
                lock(this)
                {
                    ...
                    if (_config == null)
                    {
                        string configFile = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;
                        if (configFile != null)
                        {
                            configFile = Path.ChangeExtension(configFile, ".nlog");
                            if (File.Exists(configFile))
                            {
                                InternalLogger.Debug("Attempting to load config from {0}", configFile);
                                _config = new XmlLoggingConfiguration(configFile);
                            }
------------------ begin new code ------------------
                            else
                            {
                                // If we already checked for app.exe.nlog,
                                int exeNlogIdx = configFile.LastIndexOf(".exe.nlog", StringComparision.OrdinalIgnoreCase);
                                if (exeNlogIdx != -1)
                                {
                                    // Now check for app.nlog
                                    configFile = configFile.Remove(exeNlogIdx, 4);
                                }
                                else // we already checked for app.nlog
                                {
                                    // Now check for app.exe.nlog
                                    configFile = Path.ChangeExtension(configFile, "exe.nlog");
                                }
                                if (File.Exists(configFile))
                                {
                                    InternalLogger.Debug("Attempting to load config from {0}", configFile);
                                    _config = new XmlLoggingConfiguration(configFile);
                                }
                            }
------------------ end new code ------------------
                        }
                    }
                    ...
                }
            }

Here's the Microsoft bug report

David