Ejemplo n.º 1
0
        /// <summary>
        /// Log to the internal (NLog) logger the information about the <see cref="Target"/> and <see
        /// cref="LoggingRule"/> associated with this <see cref="LoggingConfiguration"/> instance.
        /// </summary>
        /// <remarks>
        /// The information are only recorded in the internal logger if Debug level is enabled, otherwise nothing is
        /// recorded.
        /// </remarks>
        internal void Dump()
        {
            if (!InternalLogger.IsDebugEnabled)
            {
                return;
            }

            InternalLogger.Debug("--- NLog configuration dump ---");
            InternalLogger.Debug("Targets:");
            var targetList = _targets.Values.ToList();

            foreach (Target target in targetList)
            {
                InternalLogger.Debug("{0}", target);
            }

            InternalLogger.Debug("Rules:");
            var loggingRules = LoggingRules.ToList();

            foreach (LoggingRule rule in loggingRules)
            {
                InternalLogger.Debug("{0}", rule);
            }

            InternalLogger.Debug("--- End of NLog configuration dump ---");
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Removes the specified named logging rule.
        /// </summary>
        /// <param name="ruleName">The name of the logging rule to be removed.</param>
        /// <returns>Found one or more logging rule to remove, or <see langword="false"/> when not found.</returns>
        public bool RemoveRuleByName(string ruleName)
        {
            if (ruleName == null)
            {
                return(false);
            }

            HashSet <LoggingRule> removedRules = new HashSet <LoggingRule>();
            var loggingRules = GetLoggingRulesThreadSafe();

            foreach (var loggingRule in loggingRules)
            {
                if (string.Equals(loggingRule.RuleName, ruleName, StringComparison.OrdinalIgnoreCase))
                {
                    removedRules.Add(loggingRule);
                }
            }

            if (removedRules.Count > 0)
            {
                lock (LoggingRules)
                {
                    for (int i = LoggingRules.Count - 1; i >= 0; i--)
                    {
                        if (removedRules.Contains(LoggingRules[i]))
                        {
                            LoggingRules.RemoveAt(i);
                        }
                    }
                }
            }

            return(removedRules.Count > 0);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Validates the configuration.
        /// </summary>
        internal void ValidateConfig()
        {
            var roots = new List <object>();

            var loggingRules = LoggingRules.ToList();

            foreach (LoggingRule rule in loggingRules)
            {
                roots.Add(rule);
            }

            var targetList = _targets.Values.ToList();

            foreach (Target target in targetList)
            {
                roots.Add(target);
            }

            _configItems = ObjectGraphScanner.FindReachableObjects <object>(true, roots.ToArray());

            // initialize all config items starting from most nested first
            // so that whenever the container is initialized its children have already been
            InternalLogger.Info("Found {0} configuration items", _configItems.Count);

            foreach (object o in _configItems)
            {
                PropertyHelper.CheckRequiredParameters(o);
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Add a rule for alle loglevels.
        /// </summary>
        /// <param name="target">Target to be written to when the rule matches.</param>
        /// <param name="loggerNamePattern">Logger name pattern. It may include the '*' wildcard at the beginning, at the end or at both ends.</param>
        public void AddRuleForAllLevels(Target target, string loggerNamePattern = "*")
        {
            var loggingRule = new LoggingRule(loggerNamePattern, target);

            loggingRule.EnableLoggingForLevels(LogLevel.MinLevel, LogLevel.MaxLevel);
            LoggingRules.Add(loggingRule);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Add a rule for one loglevel.
        /// </summary>
        /// <param name="level">log level needed to trigger this rule. </param>
        /// <param name="target">Target to be written to when the rule matches.</param>
        /// <param name="loggerNamePattern">Logger name pattern. It may include the '*' wildcard at the beginning, at the end or at both ends.</param>
        public void AddRuleForOneLevel(LogLevel level, Target target, string loggerNamePattern = "*")
        {
            var loggingRule = new LoggingRule(loggerNamePattern, target);

            loggingRule.EnableLoggingForLevel(level);
            LoggingRules.Add(loggingRule);
        }
        public DefaultNLogConfiguration()
        {
            var consoleTarget = new ColoredConsoleTarget {
                Layout = @"${date:format=HH\:mm\:ss} ${logger} ${message}"
            };
            var rule1 = new LoggingRule("*", LogLevel.Trace, consoleTarget);

            AddTarget("_default_console", consoleTarget);
            LoggingRules.Add(rule1);
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Create a host and initialize the environment.
        /// </summary>
        /// <param name="builder"></param>
        public void Init(HostEnvironment.Builder builder)
        {
            var hostEnvironment = builder.Create();

            // setup any logging rules
            LoggingRules.SetRules(LoggingRulesList.Create(false, hostEnvironment.LoggingRules));

            // create the main build host
            BuildHost.CreateHost(hostEnvironment);

            // now hang on, we need to do the appropriate registration and setup
            Logging.Set(hostEnvironment.Logger);

            // set the default collection comparer for children
            PrimitiveCollectionComparer.SetDefault(hostEnvironment.DefaultCollectionComparer);
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Flushes any pending log messages on all appenders.
        /// </summary>
        /// <param name="asyncContinuation">The asynchronous continuation.</param>
        internal void FlushAllTargets(AsyncContinuation asyncContinuation)
        {
            InternalLogger.Trace("Flushing all targets...");

            var uniqueTargets = new List <Target>();
            var loggingRules  = LoggingRules.ToList();

            foreach (var rule in loggingRules)
            {
                var targetList = rule.Targets.ToList();
                foreach (var target in targetList)
                {
                    if (!uniqueTargets.Contains(target))
                    {
                        uniqueTargets.Add(target);
                    }
                }
            }

            AsyncHelpers.ForEachItemInParallel(uniqueTargets, asyncContinuation, (target, cont) => target.Flush(cont));
        }
Ejemplo n.º 9
0
 public TestLoggingConfiguration()
 {
     Target = new TestTarget();
     LoggingRules.Add(new LoggingRule("*", LogLevel.Trace, Target));
 }
Ejemplo n.º 10
0
 /// <summary>
 /// Add a rule with min- and maxLevel.
 /// </summary>
 /// <param name="minLevel">Minimum log level needed to trigger this rule.</param>
 /// <param name="maxLevel">Maximum log level needed to trigger this rule.</param>
 /// <param name="target">Target to be written to when the rule matches.</param>
 /// <param name="loggerNamePattern">Logger name pattern. It may include the '*' wildcard at the beginning, at the end or at both ends.</param>
 public void AddRule(LogLevel minLevel, LogLevel maxLevel, Target target, string loggerNamePattern = "*")
 {
     LoggingRules.Add(new LoggingRule(loggerNamePattern, minLevel, maxLevel, target));
 }