Example #1
0
        /// <summary>
        /// Updates all the parents of the specified logger
        /// </summary>
        /// <param name="log">The logger to update the parents for</param>
        /// <remarks>
        /// <para>
        /// This method loops through all the <i>potential</i> parents of
        /// <paramref name="log"/>. There 3 possible cases:
        /// </para>
        /// <list type="number">
        ///		<item>
        ///			<term>No entry for the potential parent of <paramref name="log"/> exists</term>
        ///			<description>
        ///			We create a ProvisionNode for this potential
        ///			parent and insert <paramref name="log"/> in that provision node.
        ///			</description>
        ///		</item>
        ///		<item>
        ///			<term>The entry is of type Logger for the potential parent.</term>
        ///			<description>
        ///			The entry is <paramref name="log"/>'s nearest existing parent. We
        ///			update <paramref name="log"/>'s parent field with this entry. We also break from
        ///			he loop because updating our parent's parent is our parent's
        ///			responsibility.
        ///			</description>
        ///		</item>
        ///		<item>
        ///			<term>The entry is of type ProvisionNode for this potential parent.</term>
        ///			<description>
        ///			We add <paramref name="log"/> to the list of children for this
        ///			potential parent.
        ///			</description>
        ///		</item>
        /// </list>
        /// </remarks>
        private void UpdateParents(Logger log)
        {
            string name        = log.Name;
            int    length      = name.Length;
            bool   parentFound = false;

            // if name = "w.x.y.z", loop through "w.x.y", "w.x" and "w", but not "w.x.y.z"
            for (int i = name.LastIndexOf('.', length - 1); i >= 0; i = name.LastIndexOf('.', i - 1))
            {
                string substr = name.Substring(0, i);

                LoggerKey key  = new LoggerKey(substr);                // simple constructor
                Object    node = m_ht[key];
                // Create a provision node for a future parent.
                if (node == null)
                {
                    ProvisionNode pn = new ProvisionNode(log);
                    m_ht[key] = pn;
                }
                else
                {
                    Logger nodeLogger = node as Logger;
                    if (nodeLogger != null)
                    {
                        parentFound = true;
                        log.Parent  = nodeLogger;
                        break;                         // no need to update the ancestors of the closest ancestor
                    }
                    else
                    {
                        ProvisionNode nodeProvisionNode = node as ProvisionNode;
                        if (nodeProvisionNode != null)
                        {
                            nodeProvisionNode.Add(log);
                        }
                        else
                        {
                            LogLog.Error(declaringType, "Unexpected object type [" + node.GetType() + "] in ht.", new LogException());
                        }
                    }
                }
            }

            // If we could not find any existing parents, then link with root.
            if (!parentFound)
            {
                log.Parent = this.Root;
            }
        }