public void Matches_should_return_true_when_rule_has_no_source_or_operation_scope()
        {
            var rule = new LogConfigurationRule();

            rule.Matches(Event()).Should().BeTrue();
            rule.Matches(Event("src", "op")).Should().BeTrue();
        }
        public void Allows_should_return_false_when_rule_does_not_restrict_logging()
        {
            var rule = new LogConfigurationRule();

            rule.Allows(Event(LogLevel.Debug)).Should().BeTrue();
            rule.Allows(Event(LogLevel.Fatal)).Should().BeTrue();
        }
        public void Should_order_by_decreasing_rule_permissiveness_within_same_specificity()
        {
            var rule1 = new LogConfigurationRule {
                Enabled = false
            };
            var rule2 = new LogConfigurationRule {
                Enabled = true
            };
            var rule3 = new LogConfigurationRule {
                Enabled = true, MinimumLevel = LogLevel.Fatal
            };
            var rule4 = new LogConfigurationRule {
                Enabled = true, MinimumLevel = LogLevel.Error
            };
            var rule5 = new LogConfigurationRule {
                Enabled = true, MinimumLevel = LogLevel.Warn
            };
            var rule6 = new LogConfigurationRule {
                Enabled = true, MinimumLevel = LogLevel.Info
            };

            RuleOrderingHelper
            .Order(new[] { rule1, rule2, rule3, rule4, rule5, rule6 })
            .Should().Equal(rule2, rule6, rule5, rule4, rule3, rule1);
        }
        public void Allows_should_return_false_when_rule_disables_logging()
        {
            var rule = new LogConfigurationRule {
                Enabled = false
            };

            rule.Allows(Event(LogLevel.Info)).Should().BeFalse();
            rule.Allows(Event(LogLevel.Fatal)).Should().BeFalse();
        }
        public void Allows_should_return_false_when_rule_prohibits_event_level()
        {
            var rule = new LogConfigurationRule {
                MinimumLevel = LogLevel.Warn
            };

            rule.Allows(Event(LogLevel.Debug)).Should().BeFalse();
            rule.Allows(Event(LogLevel.Info)).Should().BeFalse();
        }
        public void Allows_should_return_false_when_rule_permits_event_level()
        {
            var rule = new LogConfigurationRule {
                MinimumLevel = LogLevel.Warn
            };

            rule.Allows(Event(LogLevel.Warn)).Should().BeTrue();
            rule.Allows(Event(LogLevel.Error)).Should().BeTrue();
            rule.Allows(Event(LogLevel.Fatal)).Should().BeTrue();
        }
        public void Matches_should_handle_source_context_requirement()
        {
            var rule = new LogConfigurationRule {
                Source = "src"
            };

            rule.Matches(Event()).Should().BeFalse();
            rule.Matches(Event("some-other-source")).Should().BeFalse();

            rule.Matches(Event("src")).Should().BeTrue();
            rule.Matches(Event("SRC")).Should().BeTrue();
            rule.Matches(Event("Src-with-suffix", "op")).Should().BeTrue();
        }
        public void Matches_should_handle_operation_context_requirement()
        {
            var rule = new LogConfigurationRule {
                Operation = "op"
            };

            rule.Matches(Event()).Should().BeFalse();
            rule.Matches(Event(operationContext: "some-other-op")).Should().BeFalse();

            rule.Matches(Event(operationContext: "op")).Should().BeTrue();
            rule.Matches(Event(operationContext: "OP")).Should().BeTrue();
            rule.Matches(Event("src", "Op-with-suffix")).Should().BeTrue();
        }
Exemple #9
0
        private static int GetPermissiveness(LogConfigurationRule rule)
        {
            if (!rule.Enabled)
            {
                return(0);
            }

            if (rule.MinimumLevel == null)
            {
                return(MaxPermissiveness);
            }

            return(MaxPermissiveness - (int)rule.MinimumLevel.Value);
        }
        public void Matches_should_handle_source_and_operation_context_requirements()
        {
            var rule = new LogConfigurationRule {
                Source = "src", Operation = "op"
            };

            rule.Matches(Event()).Should().BeFalse();
            rule.Matches(Event("src")).Should().BeFalse();
            rule.Matches(Event(operationContext: "op")).Should().BeFalse();

            rule.Matches(Event("src", "op")).Should().BeTrue();
            rule.Matches(Event("SRC", "OP")).Should().BeTrue();
            rule.Matches(Event("Src-with-suffix", "Op-with-suffix")).Should().BeTrue();
        }
        public void Should_order_by_decreasing_scope_length_within_same_specificity()
        {
            var rule1 = new LogConfigurationRule {
                Source = "A.B", MinimumLevel = LogLevel.Debug
            };
            var rule2 = new LogConfigurationRule {
                Source = "A", MinimumLevel = LogLevel.Info
            };
            var rule3 = new LogConfigurationRule {
                Source = "A.B.C", MinimumLevel = LogLevel.Warn
            };

            RuleOrderingHelper
            .Order(new[] { rule1, rule2, rule3 })
            .Should().Equal(rule3, rule1, rule2);
        }
Exemple #12
0
        private static int GetSpecificity(LogConfigurationRule rule)
        {
            var specificity = 0;

            if (rule.HasSourceScope)
            {
                specificity += 1;
            }

            if (rule.HasOperationScope)
            {
                specificity += 2;
            }

            return(specificity);
        }
        public static LogConfigurationRule WithLog(this LogConfigurationRule rule, string log)
        {
            if (rule.Log != log && rule.Log != null)
            {
                throw new ArgumentException($"Rule for {rule.Log} log can't be used for {log} log.");
            }

            rule = new LogConfigurationRule
            {
                Enabled      = rule.Enabled,
                Log          = log,
                Operation    = rule.Operation,
                Properties   = rule.Properties,
                Source       = rule.Source,
                MinimumLevel = rule.MinimumLevel
            };

            return(rule);
        }
        public void Should_order_by_decreasing_rule_specificity()
        {
            var rule1 = new LogConfigurationRule {
                Log = "log", MinimumLevel = LogLevel.Info
            };
            var rule2 = new LogConfigurationRule {
                Log = "log", Source = "src", MinimumLevel = LogLevel.Warn
            };
            var rule3 = new LogConfigurationRule {
                Log = "log", Operation = "op", MinimumLevel = LogLevel.Error
            };
            var rule4 = new LogConfigurationRule {
                Log = "log", Source = "src", Operation = "op", MinimumLevel = LogLevel.Fatal
            };

            RuleOrderingHelper
            .Order(new[] { rule1, rule2, rule3, rule4 })
            .Should().Equal(rule4, rule3, rule2, rule1);
        }
        private static bool HasMatchingOperationContext(LogConfigurationRule rule, LogEvent @event)
        {
            if (!rule.HasOperationScope)
            {
                return(true);
            }

            if (@event.Properties == null)
            {
                return(false);
            }

            if ([email protected](WellKnownProperties.OperationContext, out var operationContextValue))
            {
                return(false);
            }

            if (!(operationContextValue is OperationContextValue operationContext))
            {
                return(false);
            }

            return(operationContext.Any(value => value.StartsWith(rule.Operation, StringComparison.OrdinalIgnoreCase)));
        }
 public static bool Matches([NotNull] this LogConfigurationRule rule, [NotNull] LogEvent @event)
 => HasMatchingSourceContext(rule, @event) && HasMatchingOperationContext(rule, @event);
 public static bool Allows([NotNull] this LogConfigurationRule rule, [NotNull] LogEvent @event)
 => rule.Enabled && (rule.MinimumLevel == null || @event.Level >= rule.MinimumLevel.Value);
Exemple #18
0
 public IVostokConsoleLogBuilder AddRule(LogConfigurationRule rule)
 {
     rule = rule.WithLog(Logs.ConsoleLogName);
     rulesBuilder.Add(rule);
     return(this);
 }
Exemple #19
0
 public void Add([NotNull] LogConfigurationRule rule)
 => userRules.Add(rule ?? throw new ArgumentNullException(nameof(rule)));