private void ParseNLogElement(NLogXmlElement nlogElement, string baseDirectory)
        {
            InternalLogger.Trace("ParseNLogElement");
            nlogElement.AssertName("nlog");

            AutoReload = nlogElement.GetOptionalBooleanAttribute("autoReload", false);
            LogManager.ThrowExceptions  = nlogElement.GetOptionalBooleanAttribute("throwExceptions", LogManager.ThrowExceptions);
            InternalLogger.LogToConsole = nlogElement.GetOptionalBooleanAttribute("internalLogToConsole", InternalLogger.LogToConsole);
#if !NET_CF
            InternalLogger.LogToConsoleError = nlogElement.GetOptionalBooleanAttribute("internalLogToConsoleError", InternalLogger.LogToConsoleError);
#endif
            InternalLogger.LogFile     = nlogElement.GetOptionalAttribute("internalLogFile", InternalLogger.LogFile);
            InternalLogger.LogLevel    = LogLevel.FromString(nlogElement.GetOptionalAttribute("internalLogLevel", InternalLogger.LogLevel.Name));
            LogManager.GlobalThreshold = LogLevel.FromString(nlogElement.GetOptionalAttribute("globalThreshold", LogManager.GlobalThreshold.Name));

            foreach (var el in nlogElement.Children)
            {
                switch (el.LocalName.ToUpper(CultureInfo.InvariantCulture))
                {
                case "EXTENSIONS":
                    ParseExtensionsElement(el, baseDirectory);
                    break;

                case "INCLUDE":
                    ParseIncludeElement(el, baseDirectory);
                    break;

                case "APPENDERS":
                case "TARGETS":
                    ParseTargetsElement(el);
                    break;

                case "VARIABLE":
                    ParseVariableElement(el);
                    break;

                case "RULES":
                    ParseRulesElement(el, LoggingRules);
                    break;

                default:
                    InternalLogger.Warn("Skipping unknown node: {0}", el.LocalName);
                    break;
                }
            }
        }
        private void ParseIncludeElement(NLogXmlElement includeElement, string baseDirectory)
        {
            includeElement.AssertName("include");

            var newFileName = includeElement.GetRequiredAttribute("file");

            try
            {
                newFileName = ExpandVariables(newFileName);
                newFileName = SimpleLayout.Evaluate(newFileName);
                if (baseDirectory != null)
                {
                    newFileName = Path.Combine(baseDirectory, newFileName);
                }

#if SILVERLIGHT
                newFileName = newFileName.Replace("\\", "/");
                if (Application.GetResourceStream(new Uri(newFileName, UriKind.Relative)) != null)
#else
                if (File.Exists(newFileName))
#endif
                {
                    InternalLogger.Debug("Including file '{0}'", newFileName);
                    ConfigureFromFile(newFileName);
                }
                else
                {
                    throw new FileNotFoundException("Included file not found: " + newFileName);
                }
            }
            catch (Exception exception)
            {
                if (exception.MustBeRethrown())
                {
                    throw;
                }

                InternalLogger.Error("Error when including '{0}' {1}", newFileName, exception);

                if (includeElement.GetOptionalBooleanAttribute("ignoreErrors", false))
                {
                    return;
                }

                throw new NLogConfigurationException("Error when including: " + newFileName, exception);
            }
        }
        private void ParseTargetsElement(NLogXmlElement targetsElement)
        {
            targetsElement.AssertName("targets", "appenders");

            var            asyncWrap                         = targetsElement.GetOptionalBooleanAttribute("async", false);
            NLogXmlElement defaultWrapperElement             = null;
            var            typeNameToDefaultTargetParameters = new Dictionary <string, NLogXmlElement>();

            foreach (var targetElement in targetsElement.Children)
            {
                var name = targetElement.LocalName;
                var type = StripOptionalNamespacePrefix(targetElement.GetOptionalAttribute("type", null));

                switch (name.ToUpper(CultureInfo.InvariantCulture))
                {
                case "DEFAULT-WRAPPER":
                    defaultWrapperElement = targetElement;
                    break;

                case "DEFAULT-TARGET-PARAMETERS":
                    if (type == null)
                    {
                        throw new NLogConfigurationException("Missing 'type' attribute on <" + name + "/>.");
                    }

                    typeNameToDefaultTargetParameters[type] = targetElement;
                    break;

                case "TARGET":
                case "APPENDER":
                case "WRAPPER":
                case "WRAPPER-TARGET":
                case "COMPOUND-TARGET":
                    if (type == null)
                    {
                        throw new NLogConfigurationException("Missing 'type' attribute on <" + name + "/>.");
                    }

                    var newTarget = configurationItemFactory.Targets.CreateInstance(type);

                    NLogXmlElement defaults;
                    if (typeNameToDefaultTargetParameters.TryGetValue(type, out defaults))
                    {
                        ParseTargetElement(newTarget, defaults);
                    }

                    ParseTargetElement(newTarget, targetElement);

                    if (asyncWrap)
                    {
                        newTarget = WrapWithAsyncTargetWrapper(newTarget);
                    }

                    if (defaultWrapperElement != null)
                    {
                        newTarget = WrapWithDefaultWrapper(newTarget, defaultWrapperElement);
                    }

                    InternalLogger.Info("Adding target {0}", newTarget);
                    AddTarget(newTarget.Name, newTarget);
                    break;
                }
            }
        }
        private void ParseLoggerElement(NLogXmlElement loggerElement, IList <LoggingRule> rulesCollection)
        {
            loggerElement.AssertName("logger");

            var rule        = new LoggingRule();
            var namePattern = loggerElement.GetOptionalAttribute("name", "*");
            var appendTo    = loggerElement.GetOptionalAttribute("appendTo", null);

            if (appendTo == null)
            {
                appendTo = loggerElement.GetOptionalAttribute("writeTo", null);
            }

            rule.LoggerNamePattern = namePattern;
            if (appendTo != null)
            {
                foreach (var t in appendTo.Split(','))
                {
                    var targetName = t.Trim();
                    var target     = FindTargetByName(targetName);

                    if (target != null)
                    {
                        rule.Targets.Add(target);
                    }
                    else
                    {
                        throw new NLogConfigurationException("Target " + targetName + " not found.");
                    }
                }
            }

            rule.Final = loggerElement.GetOptionalBooleanAttribute("final", false);

            string levelString;

            if (loggerElement.AttributeValues.TryGetValue("level", out levelString))
            {
                var level = LogLevel.FromString(levelString);
                rule.EnableLoggingForLevel(level);
            }
            else if (loggerElement.AttributeValues.TryGetValue("levels", out levelString))
            {
                levelString = CleanWhitespace(levelString);

                var tokens = levelString.Split(',');
                foreach (var s in tokens)
                {
                    if (!string.IsNullOrEmpty(s))
                    {
                        var level = LogLevel.FromString(s);
                        rule.EnableLoggingForLevel(level);
                    }
                }
            }
            else
            {
                var    minLevel = 0;
                var    maxLevel = LogLevel.MaxLevel.Ordinal;
                string minLevelString;
                string maxLevelString;

                if (loggerElement.AttributeValues.TryGetValue("minLevel", out minLevelString))
                {
                    minLevel = LogLevel.FromString(minLevelString).Ordinal;
                }

                if (loggerElement.AttributeValues.TryGetValue("maxLevel", out maxLevelString))
                {
                    maxLevel = LogLevel.FromString(maxLevelString).Ordinal;
                }

                for (var i = minLevel; i <= maxLevel; ++i)
                {
                    rule.EnableLoggingForLevel(LogLevel.FromOrdinal(i));
                }
            }

            foreach (var child in loggerElement.Children)
            {
                switch (child.LocalName.ToUpper(CultureInfo.InvariantCulture))
                {
                case "FILTERS":
                    ParseFilters(rule, child);
                    break;

                case "LOGGER":
                    ParseLoggerElement(child, rule.ChildRules);
                    break;
                }
            }

            rulesCollection.Add(rule);
        }
        private void ParseTargetsElement(NLogXmlElement targetsElement)
        {
            targetsElement.AssertName("targets", "appenders");

            var asyncWrap = targetsElement.GetOptionalBooleanAttribute("async", false);
            NLogXmlElement defaultWrapperElement = null;
            var typeNameToDefaultTargetParameters = new Dictionary<string, NLogXmlElement>();

            foreach (var targetElement in targetsElement.Children)
            {
                var name = targetElement.LocalName;
                var type = StripOptionalNamespacePrefix(targetElement.GetOptionalAttribute("type", null));

                switch (name.ToUpper(CultureInfo.InvariantCulture))
                {
                    case "DEFAULT-WRAPPER":
                        defaultWrapperElement = targetElement;
                        break;

                    case "DEFAULT-TARGET-PARAMETERS":
                        if (type == null)
                        {
                            throw new NLogConfigurationException("Missing 'type' attribute on <" + name + "/>.");
                        }

                        typeNameToDefaultTargetParameters[type] = targetElement;
                        break;

                    case "TARGET":
                    case "APPENDER":
                    case "WRAPPER":
                    case "WRAPPER-TARGET":
                    case "COMPOUND-TARGET":
                        if (type == null)
                        {
                            throw new NLogConfigurationException("Missing 'type' attribute on <" + name + "/>.");
                        }

                        var newTarget = configurationItemFactory.Targets.CreateInstance(type);

                        NLogXmlElement defaults;
                        if (typeNameToDefaultTargetParameters.TryGetValue(type, out defaults))
                        {
                            ParseTargetElement(newTarget, defaults);
                        }

                        ParseTargetElement(newTarget, targetElement);

                        if (asyncWrap)
                        {
                            newTarget = WrapWithAsyncTargetWrapper(newTarget);
                        }

                        if (defaultWrapperElement != null)
                        {
                            newTarget = WrapWithDefaultWrapper(newTarget, defaultWrapperElement);
                        }

                        InternalLogger.Info("Adding target {0}", newTarget);
                        AddTarget(newTarget.Name, newTarget);
                        break;
                }
            }
        }
        private void ParseNLogElement(NLogXmlElement nlogElement, string baseDirectory)
        {
            InternalLogger.Trace("ParseNLogElement");
            nlogElement.AssertName("nlog");

            AutoReload = nlogElement.GetOptionalBooleanAttribute("autoReload", false);
            LogManager.ThrowExceptions = nlogElement.GetOptionalBooleanAttribute("throwExceptions", LogManager.ThrowExceptions);
            InternalLogger.LogToConsole = nlogElement.GetOptionalBooleanAttribute("internalLogToConsole", InternalLogger.LogToConsole);
            #if !NET_CF
            InternalLogger.LogToConsoleError = nlogElement.GetOptionalBooleanAttribute("internalLogToConsoleError", InternalLogger.LogToConsoleError);
            #endif
            InternalLogger.LogFile = nlogElement.GetOptionalAttribute("internalLogFile", InternalLogger.LogFile);
            InternalLogger.LogLevel = LogLevel.FromString(nlogElement.GetOptionalAttribute("internalLogLevel", InternalLogger.LogLevel.Name));
            LogManager.GlobalThreshold = LogLevel.FromString(nlogElement.GetOptionalAttribute("globalThreshold", LogManager.GlobalThreshold.Name));

            foreach (var el in nlogElement.Children)
            {
                switch (el.LocalName.ToUpper(CultureInfo.InvariantCulture))
                {
                    case "EXTENSIONS":
                        ParseExtensionsElement(el, baseDirectory);
                        break;

                    case "INCLUDE":
                        ParseIncludeElement(el, baseDirectory);
                        break;

                    case "APPENDERS":
                    case "TARGETS":
                        ParseTargetsElement(el);
                        break;

                    case "VARIABLE":
                        ParseVariableElement(el);
                        break;

                    case "RULES":
                        ParseRulesElement(el, LoggingRules);
                        break;

                    default:
                        InternalLogger.Warn("Skipping unknown node: {0}", el.LocalName);
                        break;
                }
            }
        }
        private void ParseLoggerElement(NLogXmlElement loggerElement, IList<LoggingRule> rulesCollection)
        {
            loggerElement.AssertName("logger");

            var rule = new LoggingRule();
            var namePattern = loggerElement.GetOptionalAttribute("name", "*");
            var appendTo = loggerElement.GetOptionalAttribute("appendTo", null);
            if (appendTo == null)
            {
                appendTo = loggerElement.GetOptionalAttribute("writeTo", null);
            }

            rule.LoggerNamePattern = namePattern;
            if (appendTo != null)
            {
                foreach (var t in appendTo.Split(','))
                {
                    var targetName = t.Trim();
                    var target = FindTargetByName(targetName);

                    if (target != null)
                    {
                        rule.Targets.Add(target);
                    }
                    else
                    {
                        throw new NLogConfigurationException("Target " + targetName + " not found.");
                    }
                }
            }

            rule.Final = loggerElement.GetOptionalBooleanAttribute("final", false);

            string levelString;

            if (loggerElement.AttributeValues.TryGetValue("level", out levelString))
            {
                var level = LogLevel.FromString(levelString);
                rule.EnableLoggingForLevel(level);
            }
            else if (loggerElement.AttributeValues.TryGetValue("levels", out levelString))
            {
                levelString = CleanWhitespace(levelString);

                var tokens = levelString.Split(',');
                foreach (var s in tokens)
                {
                    if (!string.IsNullOrEmpty(s))
                    {
                        var level = LogLevel.FromString(s);
                        rule.EnableLoggingForLevel(level);
                    }
                }
            }
            else
            {
                var minLevel = 0;
                var maxLevel = LogLevel.MaxLevel.Ordinal;
                string minLevelString;
                string maxLevelString;

                if (loggerElement.AttributeValues.TryGetValue("minLevel", out minLevelString))
                {
                    minLevel = LogLevel.FromString(minLevelString).Ordinal;
                }

                if (loggerElement.AttributeValues.TryGetValue("maxLevel", out maxLevelString))
                {
                    maxLevel = LogLevel.FromString(maxLevelString).Ordinal;
                }

                for (var i = minLevel; i <= maxLevel; ++i)
                {
                    rule.EnableLoggingForLevel(LogLevel.FromOrdinal(i));
                }
            }

            foreach (var child in loggerElement.Children)
            {
                switch (child.LocalName.ToUpper(CultureInfo.InvariantCulture))
                {
                    case "FILTERS":
                        ParseFilters(rule, child);
                        break;

                    case "LOGGER":
                        ParseLoggerElement(child, rule.ChildRules);
                        break;
                }
            }

            rulesCollection.Add(rule);
        }
        private void ParseIncludeElement(NLogXmlElement includeElement, string baseDirectory)
        {
            includeElement.AssertName("include");

            var newFileName = includeElement.GetRequiredAttribute("file");

            try
            {
                newFileName = ExpandVariables(newFileName);
                newFileName = SimpleLayout.Evaluate(newFileName);
                if (baseDirectory != null)
                {
                    newFileName = Path.Combine(baseDirectory, newFileName);
                }

            #if SILVERLIGHT
                newFileName = newFileName.Replace("\\", "/");
                if (Application.GetResourceStream(new Uri(newFileName, UriKind.Relative)) != null)
            #else
                if (File.Exists(newFileName))
            #endif
                {
                    InternalLogger.Debug("Including file '{0}'", newFileName);
                    ConfigureFromFile(newFileName);
                }
                else
                {
                    throw new FileNotFoundException("Included file not found: " + newFileName);
                }
            }
            catch (Exception exception)
            {
                if (exception.MustBeRethrown())
                {
                    throw;
                }

                InternalLogger.Error("Error when including '{0}' {1}", newFileName, exception);

                if (includeElement.GetOptionalBooleanAttribute("ignoreErrors", false))
                {
                    return;
                }

                throw new NLogConfigurationException("Error when including: " + newFileName, exception);
            }
        }