/// <summary>
 /// Converts a string value to a target type.
 /// </summary>
 /// <param name="type">The type of object to convert the string to.</param>
 /// <param name="value">The string value to use as the value of the object.</param>
 /// <returns>
 /// An object of type <paramref name="type"/> with value <paramref name="value"/> or
 /// <c>null</c> when the conversion could not be performed.
 /// </returns>
 protected object ConvertStringTo(Type type, string value)
 {
     // Hack to allow use of Level in property
     if (type.IsAssignableFrom(typeof(Level)))
     {
         // Property wants a level
         return(m_hierarchy.LevelMap[value]);
     }
     return(OptionConverter.ConvertStringTo(type, value));
 }
Пример #2
0
 /// <summary>
 /// Converts a string value to a target type.
 /// </summary>
 /// <param name="type">The type of object to convert the string to.</param>
 /// <param name="value">The string value to use as the value of the object.</param>
 /// <returns>
 /// <para>
 /// An object of type <paramref name="type" /> with value <paramref name="value" /> or
 /// <c>null</c> when the conversion could not be performed.
 /// </para>
 /// </returns>
 protected object ConvertStringTo(Type type, string value)
 {
     if ((object)typeof(Level) == type)
     {
         Level level = m_hierarchy.LevelMap[value];
         if (level == null)
         {
             LogLog.Error(declaringType, "XmlHierarchyConfigurator: Unknown Level Specified [" + value + "]");
         }
         return(level);
     }
     return(OptionConverter.ConvertStringTo(type, value));
 }
Пример #3
0
        /// <summary>
        /// Converts a string value to a target type.
        /// </summary>
        /// <param name="type">The type of object to convert the string to.</param>
        /// <param name="value">The string value to use as the value of the object.</param>
        /// <returns>
        /// <para>
        /// An object of type <paramref name="type"/> with value <paramref name="value"/> or
        /// <c>null</c> when the conversion could not be performed.
        /// </para>
        /// </returns>
        protected object ConvertStringTo(Type type, string value)
        {
            // Hack to allow use of Level in property
            if (typeof(Level) == type)
            {
                // Property wants a level
                Level levelValue = m_hierarchy.LevelMap[value];

                if (levelValue == null)
                {
                    LogLog.Error(declaringType, "XmlHierarchyConfigurator: Unknown Level Specified [" + value + "]");
                }

                return(levelValue);
            }
            return(OptionConverter.ConvertStringTo(type, value));
        }
Пример #4
0
        /// <summary>
        /// Configure the hierarchy by parsing a DOM tree of XML elements.
        /// </summary>
        /// <param name="element">The root element to parse.</param>
        /// <remarks>
        /// <para>
        /// Configure the hierarchy by parsing a DOM tree of XML elements.
        /// </para>
        /// </remarks>
        public void Configure(XmlElement element)
        {
            if (element == null || m_hierarchy == null)
            {
                return;
            }

            string rootElementName = element.LocalName;

            if (rootElementName != CONFIGURATION_TAG)
            {
                LogLog.Error(declaringType, "Xml element is - not a <" + CONFIGURATION_TAG + "> element.");
                return;
            }

            if (!LogLog.EmitInternalMessages)
            {
                // Look for a emitDebug attribute to enable internal debug
                string emitDebugAttribute = element.GetAttribute(EMIT_INTERNAL_DEBUG_ATTR);
                LogLog.Debug(declaringType, EMIT_INTERNAL_DEBUG_ATTR + " attribute [" + emitDebugAttribute + "].");

                if (emitDebugAttribute.Length > 0 && emitDebugAttribute != "null")
                {
                    LogLog.EmitInternalMessages = OptionConverter.ToBoolean(emitDebugAttribute, true);
                }
                else
                {
                    LogLog.Debug(declaringType, "Ignoring " + EMIT_INTERNAL_DEBUG_ATTR + " attribute.");
                }
            }

            if (!LogLog.InternalDebugging)
            {
                // Look for a debug attribute to enable internal debug
                string debugAttribute = element.GetAttribute(INTERNAL_DEBUG_ATTR);
                LogLog.Debug(declaringType, INTERNAL_DEBUG_ATTR + " attribute [" + debugAttribute + "].");

                if (debugAttribute.Length > 0 && debugAttribute != "null")
                {
                    LogLog.InternalDebugging = OptionConverter.ToBoolean(debugAttribute, true);
                }
                else
                {
                    LogLog.Debug(declaringType, "Ignoring " + INTERNAL_DEBUG_ATTR + " attribute.");
                }

                string confDebug = element.GetAttribute(CONFIG_DEBUG_ATTR);
                if (confDebug.Length > 0 && confDebug != "null")
                {
                    LogLog.Warn(declaringType, "The \"" + CONFIG_DEBUG_ATTR + "\" attribute is deprecated.");
                    LogLog.Warn(declaringType, "Use the \"" + INTERNAL_DEBUG_ATTR + "\" attribute instead.");
                    LogLog.InternalDebugging = OptionConverter.ToBoolean(confDebug, true);
                }
            }

            // Default mode is merge
            ConfigUpdateMode configUpdateMode = ConfigUpdateMode.Merge;

            // Look for the config update attribute
            string configUpdateModeAttribute = element.GetAttribute(CONFIG_UPDATE_MODE_ATTR);

            if (configUpdateModeAttribute != null && configUpdateModeAttribute.Length > 0)
            {
                // Parse the attribute
                try
                {
                    configUpdateMode = (ConfigUpdateMode)OptionConverter.ConvertStringTo(typeof(ConfigUpdateMode), configUpdateModeAttribute);
                }
                catch
                {
                    LogLog.Error(declaringType, "Invalid " + CONFIG_UPDATE_MODE_ATTR + " attribute value [" + configUpdateModeAttribute + "]");
                }
            }

            // IMPL: The IFormatProvider argument to Enum.ToString() is deprecated in .NET 2.0
            LogLog.Debug(declaringType, "Configuration update mode [" + configUpdateMode.ToString() + "].");

            // Only reset configuration if overwrite flag specified
            if (configUpdateMode == ConfigUpdateMode.Overwrite)
            {
                // Reset to original unset configuration
                m_hierarchy.ResetConfiguration();
                LogLog.Debug(declaringType, "Configuration reset before reading config.");
            }

            /* Building Appender objects, placing them in a local namespace
             * for future reference */

            /* Process all the top level elements */

            foreach (XmlNode currentNode in element.ChildNodes)
            {
                if (currentNode.NodeType == XmlNodeType.Element)
                {
                    XmlElement currentElement = (XmlElement)currentNode;

                    if (currentElement.LocalName == LOGGER_TAG)
                    {
                        ParseLogger(currentElement);
                    }
                    else if (currentElement.LocalName == CATEGORY_TAG)
                    {
                        // TODO: deprecated use of category
                        ParseLogger(currentElement);
                    }
                    else if (currentElement.LocalName == ROOT_TAG)
                    {
                        ParseRoot(currentElement);
                    }
                    else if (currentElement.LocalName == RENDERER_TAG)
                    {
                        ParseRenderer(currentElement);
                    }
                    else if (currentElement.LocalName == APPENDER_TAG)
                    {
                        // We ignore appenders in this pass. They will
                        // be found and loaded if they are referenced.
                    }
                    else
                    {
                        // Read the param tags and set properties on the hierarchy
                        SetParameter(currentElement, m_hierarchy);
                    }
                }
            }

            // Lastly set the hierarchy threshold
            string thresholdStr = element.GetAttribute(THRESHOLD_ATTR);

            LogLog.Debug(declaringType, "Hierarchy Threshold [" + thresholdStr + "]");
            if (thresholdStr.Length > 0 && thresholdStr != "null")
            {
                Level thresholdLevel = (Level)ConvertStringTo(typeof(Level), thresholdStr);
                if (thresholdLevel != null)
                {
                    m_hierarchy.Threshold = thresholdLevel;
                }
                else
                {
                    LogLog.Warn(declaringType, "Unable to set hierarchy threshold using value [" + thresholdStr + "] (with acceptable conversion types)");
                }
            }

            // Done reading config
        }
Пример #5
0
        /// <summary>
        /// Configure the hierarchy by parsing a DOM tree of XML elements.
        /// </summary>
        /// <param name="element">The root element to parse.</param>
        /// <remarks>
        /// <para>
        /// Configure the hierarchy by parsing a DOM tree of XML elements.
        /// </para>
        /// </remarks>
        public void Configure(XmlElement element)
        {
            if (element == null || m_hierarchy == null)
            {
                return;
            }
            if (element.LocalName != "log4net")
            {
                LogLog.Error(declaringType, "Xml element is - not a <log4net> element.");
                return;
            }
            if (!LogLog.EmitInternalMessages)
            {
                string attribute = element.GetAttribute("emitDebug");
                LogLog.Debug(declaringType, "emitDebug attribute [" + attribute + "].");
                if (attribute.Length > 0 && attribute != "null")
                {
                    LogLog.EmitInternalMessages = OptionConverter.ToBoolean(attribute, defaultValue: true);
                }
                else
                {
                    LogLog.Debug(declaringType, "Ignoring emitDebug attribute.");
                }
            }
            if (!LogLog.InternalDebugging)
            {
                string attribute2 = element.GetAttribute("debug");
                LogLog.Debug(declaringType, "debug attribute [" + attribute2 + "].");
                if (attribute2.Length > 0 && attribute2 != "null")
                {
                    LogLog.InternalDebugging = OptionConverter.ToBoolean(attribute2, defaultValue: true);
                }
                else
                {
                    LogLog.Debug(declaringType, "Ignoring debug attribute.");
                }
                string attribute3 = element.GetAttribute("configDebug");
                if (attribute3.Length > 0 && attribute3 != "null")
                {
                    LogLog.Warn(declaringType, "The \"configDebug\" attribute is deprecated.");
                    LogLog.Warn(declaringType, "Use the \"debug\" attribute instead.");
                    LogLog.InternalDebugging = OptionConverter.ToBoolean(attribute3, defaultValue: true);
                }
            }
            ConfigUpdateMode configUpdateMode = ConfigUpdateMode.Merge;
            string           attribute4       = element.GetAttribute("update");

            if (attribute4 != null && attribute4.Length > 0)
            {
                try
                {
                    configUpdateMode = (ConfigUpdateMode)OptionConverter.ConvertStringTo(typeof(ConfigUpdateMode), attribute4);
                }
                catch
                {
                    LogLog.Error(declaringType, "Invalid update attribute value [" + attribute4 + "]");
                }
            }
            LogLog.Debug(declaringType, "Configuration update mode [" + configUpdateMode.ToString() + "].");
            if (configUpdateMode == ConfigUpdateMode.Overwrite)
            {
                m_hierarchy.ResetConfiguration();
                LogLog.Debug(declaringType, "Configuration reset before reading config.");
            }
            foreach (XmlNode childNode in element.ChildNodes)
            {
                if (childNode.NodeType == XmlNodeType.Element)
                {
                    XmlElement xmlElement = (XmlElement)childNode;
                    if (xmlElement.LocalName == "logger")
                    {
                        ParseLogger(xmlElement);
                    }
                    else if (xmlElement.LocalName == "category")
                    {
                        ParseLogger(xmlElement);
                    }
                    else if (xmlElement.LocalName == "root")
                    {
                        ParseRoot(xmlElement);
                    }
                    else if (xmlElement.LocalName == "renderer")
                    {
                        ParseRenderer(xmlElement);
                    }
                    else if (!(xmlElement.LocalName == "appender"))
                    {
                        SetParameter(xmlElement, m_hierarchy);
                    }
                }
            }
            string attribute5 = element.GetAttribute("threshold");

            LogLog.Debug(declaringType, "Hierarchy Threshold [" + attribute5 + "]");
            if (attribute5.Length > 0 && attribute5 != "null")
            {
                Level level = (Level)ConvertStringTo(typeof(Level), attribute5);
                if (level != null)
                {
                    m_hierarchy.Threshold = level;
                }
                else
                {
                    LogLog.Warn(declaringType, "Unable to set hierarchy threshold using value [" + attribute5 + "] (with acceptable conversion types)");
                }
            }
        }
Пример #6
0
        /// <summary>
        /// Convert string option into an arrangement using <see cref="ConverterRegistry.GetConvertFrom"/>
        /// </summary>
        /// <param name="option">pattern, see <seealso cref="ConvertFrom"/> for more info on formatting</param>
        /// <returns>the arrangement instance</returns>
        protected static IArrangement GetArrangementInternal(string option)
        {
            var arrangement = OptionConverter.ConvertStringTo(typeof(IArrangement), option) as IArrangement;

            return(arrangement);
        }
Пример #7
0
 public void Configure(XmlElement element)
 {
     if ((element != null) && (this.m_hierarchy != null))
     {
         if (element.LocalName != "log4net")
         {
             LogLog.Error(declaringType, "Xml element is - not a <log4net> element.");
         }
         else
         {
             if (!LogLog.EmitInternalMessages)
             {
                 string argValue = element.GetAttribute("emitDebug");
                 LogLog.Debug(declaringType, "emitDebug attribute [" + argValue + "].");
                 if ((argValue.Length > 0) && (argValue != "null"))
                 {
                     LogLog.EmitInternalMessages = OptionConverter.ToBoolean(argValue, true);
                 }
                 else
                 {
                     LogLog.Debug(declaringType, "Ignoring emitDebug attribute.");
                 }
             }
             if (!LogLog.InternalDebugging)
             {
                 string argValue = element.GetAttribute("debug");
                 LogLog.Debug(declaringType, "debug attribute [" + argValue + "].");
                 if ((argValue.Length > 0) && (argValue != "null"))
                 {
                     LogLog.InternalDebugging = OptionConverter.ToBoolean(argValue, true);
                 }
                 else
                 {
                     LogLog.Debug(declaringType, "Ignoring debug attribute.");
                 }
                 string str4 = element.GetAttribute("configDebug");
                 if ((str4.Length > 0) && (str4 != "null"))
                 {
                     LogLog.Warn(declaringType, "The \"configDebug\" attribute is deprecated.");
                     LogLog.Warn(declaringType, "Use the \"debug\" attribute instead.");
                     LogLog.InternalDebugging = OptionConverter.ToBoolean(str4, true);
                 }
             }
             ConfigUpdateMode merge     = ConfigUpdateMode.Merge;
             string           attribute = element.GetAttribute("update");
             if ((attribute != null) && (attribute.Length > 0))
             {
                 try
                 {
                     merge = (ConfigUpdateMode)OptionConverter.ConvertStringTo(typeof(ConfigUpdateMode), attribute);
                 }
                 catch
                 {
                     LogLog.Error(declaringType, "Invalid update attribute value [" + attribute + "]");
                 }
             }
             LogLog.Debug(declaringType, "Configuration update mode [" + merge.ToString() + "].");
             if (merge == ConfigUpdateMode.Overwrite)
             {
                 this.m_hierarchy.ResetConfiguration();
                 LogLog.Debug(declaringType, "Configuration reset before reading config.");
             }
             IEnumerator enumerator = element.ChildNodes.GetEnumerator();
             try
             {
                 while (enumerator.MoveNext())
                 {
                     XmlNode current = (XmlNode)enumerator.Current;
                     if (current.NodeType == XmlNodeType.Element)
                     {
                         XmlElement loggerElement = (XmlElement)current;
                         if (loggerElement.LocalName == "logger")
                         {
                             this.ParseLogger(loggerElement);
                             continue;
                         }
                         if (loggerElement.LocalName == "category")
                         {
                             this.ParseLogger(loggerElement);
                             continue;
                         }
                         if (loggerElement.LocalName == "root")
                         {
                             this.ParseRoot(loggerElement);
                             continue;
                         }
                         if (loggerElement.LocalName == "renderer")
                         {
                             this.ParseRenderer(loggerElement);
                             continue;
                         }
                         if (loggerElement.LocalName != "appender")
                         {
                             this.SetParameter(loggerElement, this.m_hierarchy);
                         }
                     }
                 }
             }
             finally
             {
                 IDisposable disposable = enumerator as IDisposable;
                 if (disposable != null)
                 {
                     disposable.Dispose();
                 }
             }
             string str6 = element.GetAttribute("threshold");
             LogLog.Debug(declaringType, "Hierarchy Threshold [" + str6 + "]");
             if ((str6.Length > 0) && (str6 != "null"))
             {
                 Level level = (Level)this.ConvertStringTo(typeof(Level), str6);
                 if (level != null)
                 {
                     this.m_hierarchy.Threshold = level;
                 }
                 else
                 {
                     LogLog.Warn(declaringType, "Unable to set hierarchy threshold using value [" + str6 + "] (with acceptable conversion types)");
                 }
             }
         }
     }
 }