exceptions due to reading config over network

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

exceptions due to reading config over network

toddgleason
I've found a few areas in NLog 2.0 (and I think these are in NLog 1 as well) where reading the configuration causes exceptions over the network.  The problem ultimately seems to be due to bugs in .NET 4, as mentioned at

http://social.msdn.microsoft.com/Forums/en/clr/thread/1e14f665-10a3-426b-a75d-4e66354c5522

There are two methods I was able to change to make things work when running an app that uses NLog in .NET 4, where the app is on the local intranet.

InternalLogger.GetSettingString():  I changed the top of the method as follows:

            // .NET 4 has crashes with this code over the network
            // string settingValue = ConfigurationManager.AppSettings[configName];
            // This should be equivalent but safer in .NET 4.
            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            KeyValueConfigurationCollection settings = config.AppSettings.Settings;
            KeyValueConfigurationElement keyValueConfigurationElement = settings[configName];
            string settingValue = (keyValueConfigurationElement != null) ? keyValueConfigurationElement.Value : null;

XmlLoggingConfiguration.AppConfig:  This one baffled me because I could not understand how it ever returned anything other than null before due to using "as" to cast a System.Configuration class to an NLog.LoggingConfiguration class.  I modified this get accessor to read:

                // .NET 4 has crashes with this code over the network
                // Moreover, it looks as though it should always return null (the LoggingConfiguration class
                // is not related to what .NET's ConfigurationManager uses).
                // object o = System.Configuration.ConfigurationManager.GetSection("nlog");
                // return o as LoggingConfiguration;
                // This was an initial hack that worked but was not what we actually wanted
                // return new LoggingConfiguration();
                // This should be equivalent but safer in .NET 4.
                Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                return new XmlLoggingConfiguration(config.FilePath);

Please note that I also initially tried doing things such as setting assembly-level attributes, but these had side effects on the rest of the application.

Will these or equivalent changes go into the final version of NLog 2.0?

--Todd
Reply | Threaded
Open this post in threaded view
|

Re: exceptions due to reading config over network

toddgleason
If you decide to implement this way, I did also add in an optimization to cache the Configuration object that ConfigurationManager.OpenExeConfiguration() returns and to share it between InternalLogger and XmlLoggingConfiguration.  (I searched the .NET 2 code base, at any rate, and could not determine for certain whether .NET caches it already, but it looked to me like they probably didn't.)

--Todd
Reply | Threaded
Open this post in threaded view
|

Re: exceptions due to reading config over network

toddgleason
I also found that the original change I made will fail if the .config file does not exist, and changed it so that it returns new LoggingConfiguration() in that case.
Reply | Threaded
Open this post in threaded view
|

Re: exceptions due to reading config over network

Gregg
In reply to this post by toddgleason
I've reproduced this issue when trying to load application from network share.
NLog 2 beta 1 failed with app.exe.config file on network share.
SecurityException and ConfigurationErrorsException errors occur in XmlLoggingConfiguration.cs (line 152):
==
object o = System.Configuration.ConfigurationManager.GetSection("nlog");
==
This bug seems to be related to .Net 4 bug but it will be a good idea to have a fix in nLog beta 2
Reply | Threaded
Open this post in threaded view
|

Re: exceptions due to reading config over network

Matt Varblow
Any updates on this one?  It still seems to be broken with NLog 2.0