How can I read default config from embedded resource?

Topics: Logging Application Block
Jan 18, 2008 at 1:17 PM
I'm using the logging application block (LAB), and I'd like to provide a default config section if the app's config file doesn't contain one for the LAB. I use this code to see if a LAB config section already exists:

LoggingSettings settings = LoggingSettings.GetLoggingSettings(ConfigurationSourceFactory.Create());
if (settings != null) return Logger.Writer;

If a LAB config section doesn't exist, I read my default LAB config info from an embedded resource, save it to a temp file, and then use a FileConfigurationSource to load it and create a LogWriter.

string tempConfigFilePath = Path.GetTempFileName();
File.WriteAllText(tempConfigFilePath, Properties.Resources.DefaultEventLoggingConfiguration);
FileConfigurationSource configSource = new FileConfigurationSource(tempConfigFilePath);
LogWriterFactory factory = new LogWriterFactory(configSource);
LogWriter result = factory.Create();
return result;

This works, but I hate having to write the config info out to a temp file and delete it every time. Is there a way to create a configuration source that reads from an XmlReader?

I tried manually creating a LoggingSettings instance and calling its ReadXml method, but that never worked. All of the System.Configuration classes expect to read from a file. I just can't find a way to get a LoggingSettings class (which inherits from ConfigurationSection) by using an XmlReader. If I could do that, then I could use DictionaryConfigurationSource to return my default LoggingSettings section.

Thanks in advance for any help.
Jan 21, 2008 at 11:13 AM

If you don't mind specifying your default config in code, you can use a DictionaryConfigurationSource just like many tests do. Just look for the references to this class in the Logging test project for examples.