Configuring the Logging from XML held within DB

Topics: Building and extending application blocks, Logging Application Block
Mar 17, 2011 at 4:26 AM


I am in the process of configuring our EL logging settings to be pulled from  a DB rather than from the standard xml config file.

What I was hoping to do was copy the <loggingConfiguration> element and its child nodes into a TEXT field within the database. Then through a basic get operation retrieve logging configurations. Is this even possible? or does one have to serialize the class Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings (once loaded) into the respective db table. I was hoping that a user could just save the RAW <loggingConfiguration> XML into the database and be able to configure the LogWriter from those instructions. I was using the source code from sql-configuration-source-for-enterprise-library as guidance.

Am I missing something.



Mar 17, 2011 at 5:00 AM

To confirm I understand your requirement, you want to be able to configure your application's logging from a string containining the whole <loggingConfiguration> element?  If this is the case, then my answer would be no as the System.Configuration doesn't have this capability. 


Sarah Urmeneta
Global Technologies and Solutions
Avanade, Inc.

Mar 17, 2011 at 2:18 PM
Edited Mar 17, 2011 at 4:43 PM

Thanks for the quick replay Sarah, man you guys are efficient...I appreciate it :)

The requirement we have is to allow a developer / IT admin (logging admin ) to paste logging instructions within a DB table which will fed into the LogWriter on the application's start-up. We thought it would be really helpful for this logging admin. to be able to  author the logging configuration within the EL config UI tool, then cut and paste these instructions into the DB table.

Here is a segment of the code :

 public ConfigurationSection GetSection(string sectionName)
            ConfigurationSection section = null;

            _configDb.ProvideSectionXml(sectionName, (xmlReader) =>
                            var serializableSection = (SerializableConfigurationSection) Activator.CreateInstance(typeof(LoggingSettings));
                            serializableSection.ReadXml(xmlReader); //<---- serialization exception occurs here.....
                            section = serializableSection;

            return section;

where the XmlReader holds the <LoggingConfiguration> XML eg:

XmReader holds = "<loggingConfiguration name=\"\" tracingEnabled=\"true\" defaultCategory=\"General\">" +
                        "<listeners>" +
                            "<add name=\"Event Log Listener\" type=\"Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FormattedEventLogTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=null\" "+
                                "listenerDataType=\"Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FormattedEventLogTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=null\" " +
                                "source=\"Enterprise Library Logging\" formatter=\"Text Formatter\" " +
                                "log=\"Application\" machineName=\".\" traceOutputOptions=\"None\" />" +
                        "</listeners>" +
                        "<formatters>" +
                            "<add type=\"Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=null\" " +
                                "template=\"Timestamp: {timestamp}{newline}&#xA;Message: {message}{newline}&#xA;Category: {category}{newline}&#xA;Priority: {priority}{newline}&#xA;EventId: {eventid}{newline}\" " +
                                "name=\"Text Formatter\" />" +
                        "</formatters>" +
                        "<logFilters>" +
                            "<add type=\"Microsoft.Practices.EnterpriseLibrary.Logging.Filters.LogEnabledFilter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=null\" " + 
                                "enabled=\"true\" name=\"Logging Enabled Filter\" />" +
                        "</logFilters>" +
                        "<categorySources>" +
                        "<add switchValue=\"All\" name=\"General\">" +
                        "<listeners>" +
                        "<add name=\"Event Log Listener\" />" +
                        "</listeners>" +
                        "</add>" +
                        "</categorySources>" +
                        "<specialSources>" +
                        "<allEvents switchValue=\"All\" name=\"All Events\" />" +
                        "<notProcessed switchValue=\"All\" name=\"Unprocessed Category\" />" +
                        "<errors switchValue=\"All\" name=\"Logging Errors &amp; Warnings\">" +
                        "<listeners>" +
                        "<add name=\"Event Log Listener\" />" +
                        "</listeners>" +
                        "</errors>" +
                        "</specialSources>" +

 My question:

  • As the EL already has to read and serialize these Logging configurations into a LoggingSettings object (via a XML config file), surely, I can use that code to do the same thing here....the only difference being that my source is not a XML file but a string from a DB table?




Mar 18, 2011 at 2:33 AM
Edited Mar 18, 2011 at 2:42 AM
I see.  I tried the same code and also got the SerializationException.  I found though a working solution, try if this works for you:
XmlReaderSettings settings = new XmlReaderSettings();
settings.ConformanceLevel = ConformanceLevel.Auto;

using (StringReader stringReader = new StringReader(stringXmlValue))
          XmlTextReader xmlReader = new XmlTextReader(stringReader);
          xmlReader.WhitespaceHandling = WhitespaceHandling.None;

           var serializableSection = (SerializableConfigurationSection)Activator.CreateInstance(typeof(LoggingSettings));

However, this fails if the stringXmlValue only contains the <loggingConfiguration>.  I found out it works if I put the xml tag before the loggingConfiguration section:

<?xml version="1.0" encoding="utf-8" ?>

Sarah Urmeneta
Global Technologies and Solutions
Avanade, Inc.

Mar 18, 2011 at 3:57 AM

Hey Sarah

It works....thanks for your help....