/// <summary>
 ///     Configures NLog for to log to the specified target so that all messages
 ///     above and including the specified level are output.
 /// </summary>
 /// <param name="target">The target to log all messages to.</param>
 /// <param name="minLevel">The minimal logging level.</param>
 public static void ConfigureForTargetLogging(Target target, LogLevel minLevel)
 {
     var config = new LoggingConfiguration();
     var rule = new LoggingRule("*", minLevel, target);
     config.LoggingRules.Add(rule);
     LogManager.Configuration = config;
 }
        public static void ConfigureForConsoleLogging(LogLevel minLevel)
        {
            var consoleTarget = new ConsoleTarget();

            var config = new LoggingConfiguration();
            var rule = new LoggingRule("*", minLevel, consoleTarget);
            config.LoggingRules.Add(rule);
            LogManager.Configuration = config;
        }
        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 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 ParseFilters(LoggingRule rule, NLogXmlElement filtersElement)
        {
            filtersElement.AssertName("filters");

            foreach (var filterElement in filtersElement.Children)
            {
                var name = filterElement.LocalName;

                var filter = configurationItemFactory.Filters.CreateInstance(name);
                ConfigureObjectFromAttributes(filter, filterElement, false);
                rule.Filters.Add(filter);
            }
        }