Esempio n. 1
0
 /// <summary>
 /// Checks whether the specified log event matches the condition (if any)
 /// </summary>
 /// <param name="logEvent">log event</param>
 /// <returns><see langword="true"/> if the condition is not defined or
 /// if it matches, <see langword="false"/> otherwise</returns>
 public bool CheckCondition(LogEventInfo logEvent)
 {
     if (_condition == null)
     {
         return(true);
     }
     return(true.Equals(_condition.Evaluate(logEvent)));
 }
        private static void AssertEvaluationResult(object expectedResult, string conditionText)
        {
            ConditionExpression condition = ConditionParser.ParseExpression(conditionText);
            LogEventInfo        context   = CreateWellKnownContext();
            object actualResult           = condition.Evaluate(context);

            Assert.Equal(expectedResult, actualResult);
        }
Esempio n. 3
0
        /// <summary>
        /// Checks the condition against the passed log event.
        /// If the condition is met, the log event is forwarded to
        /// the wrapped target.
        /// </summary>
        /// <param name="logEvent">Log event.</param>
        public override void Write(LogEventInfo logEvent)
        {
            object v = _condition.Evaluate(logEvent);

            if (v != null && v is bool && (bool)v)
            {
                WrappedTarget.Write(logEvent);
            }
        }
        private static bool ApplyFilter(AsyncLogEventInfo logEvent, ConditionExpression resultFilter)
        {
            object v = resultFilter.Evaluate(logEvent.LogEvent);

            if (boxedTrue.Equals(v))
            {
                return(true);
            }
            else
            {
                logEvent.Continuation(null);
                return(false);
            }
        }
Esempio n. 5
0
        /// <summary>
        /// Checks whether log event should be logged or not.
        /// </summary>
        /// <param name="logEvent">Log event.</param>
        /// <returns>
        /// <see cref="FilterResult.Ignore"/> - if the log event should be ignored<br/>
        /// <see cref="FilterResult.Neutral"/> - if the filter doesn't want to decide<br/>
        /// <see cref="FilterResult.Log"/> - if the log event should be logged<br/>
        /// </returns>
        protected internal override FilterResult Check(LogEventInfo logEvent)
        {
            if (_condition == null)
            {
                return(FilterResult.Neutral);
            }

            object val = _condition.Evaluate(logEvent);

            if (val != null && val is bool && ((bool)val))
            {
                return(Result);
            }
            else
            {
                return(FilterResult.Neutral);
            }
        }
        /// <summary>
        /// Apply the condition to the buffer
        /// </summary>
        /// <param name="logEvents"></param>
        /// <param name="resultFilter"></param>
        /// <returns></returns>
        private static List <AsyncLogEventInfo> ApplyFilter(IList <AsyncLogEventInfo> logEvents, ConditionExpression resultFilter)
        {
            var resultBuffer = new List <AsyncLogEventInfo>();

            for (int i = 0; i < logEvents.Count; ++i)
            {
                object v = resultFilter.Evaluate(logEvents[i].LogEvent);
                if (boxedTrue.Equals(v))
                {
                    resultBuffer.Add(logEvents[i]);
                }
                else
                {
                    // anything not passed down will be notified about successful completion
                    logEvents[i].Continuation(null);
                }
            }

            return(resultBuffer);
        }
        /// <summary>
        ///     Evaluates all filtering rules to find the first one that matches.
        ///     The matching rule determines the filtering condition to be applied
        ///     to all items in a buffer. If no condition matches, default filter
        ///     is applied to the array of log events.
        /// </summary>
        /// <param name="logEvents">Array of log events to be post-filtered.</param>
        protected override void Write(AsyncLogEventInfo[] logEvents)
        {
            ConditionExpression resultFilter = null;

            InternalLogger.Trace("Running {0} on {1} events", this, logEvents.Length);

            // evaluate all the rules to get the filtering condition
            for (var i = 0; i < logEvents.Length; ++i)
            {
                foreach (var rule in Rules)
                {
                    var v = rule.Exists.Evaluate(logEvents[i].LogEvent);

                    if (boxedTrue.Equals(v))
                    {
                        InternalLogger.Trace("Rule matched: {0}", rule.Exists);

                        resultFilter = rule.Filter;
                        break;
                    }
                }

                if (resultFilter != null)
                {
                    break;
                }
            }

            if (resultFilter == null)
            {
                resultFilter = DefaultFilter;
            }

            if (resultFilter == null)
            {
                WrappedTarget.WriteAsyncLogEvents(logEvents);
            }
            else
            {
                InternalLogger.Trace("Filter to apply: {0}", resultFilter);

                // apply the condition to the buffer
                var resultBuffer = new List <AsyncLogEventInfo>();

                for (var i = 0; i < logEvents.Length; ++i)
                {
                    var v = resultFilter.Evaluate(logEvents[i].LogEvent);
                    if (boxedTrue.Equals(v))
                    {
                        resultBuffer.Add(logEvents[i]);
                    }
                    else
                    {
                        // anything not passed down will be notified about successful completion
                        logEvents[i].Continuation(null);
                    }
                }

                InternalLogger.Trace("After filtering: {0} events.", resultBuffer.Count);
                if (resultBuffer.Count > 0)
                {
                    InternalLogger.Trace("Sending to {0}", WrappedTarget);
                    WrappedTarget.WriteAsyncLogEvents(resultBuffer.ToArray());
                }
            }
        }
Esempio n. 8
0
        /// <summary>
        /// Evaluates all filtering rules to find the first one that matches.
        /// The matching rule determines the filtering condition to be applied
        /// to all items in a buffer. If no condition matches, default filter
        /// is applied to the array of log events.
        /// </summary>
        /// <param name="logEvents">Array of log events to be post-filtered.</param>
        public override void Write(LogEventInfo[] logEvents)
        {
            ConditionExpression resultFilter = null;

            if (InternalLogger.IsTraceEnabled)
            {
                InternalLogger.Trace("Input: {0} events", logEvents.Length);
            }

            // evaluate all the rules to get the filtering condition

            for (int i = 0; i < logEvents.Length; ++i)
            {
                for (int j = 0; j < _rules.Count; ++j)
                {
                    object v = _rules[j].ExistsCondition.Evaluate(logEvents[i]);

                    if (v is bool && (bool)v)
                    {
                        if (InternalLogger.IsTraceEnabled)
                        {
                            InternalLogger.Trace("Rule matched: {0}", _rules[j].ExistsCondition);
                        }
                        resultFilter = _rules[j].FilterCondition;
                        break;
                    }
                }
                if (resultFilter != null)
                {
                    break;
                }
            }

            if (resultFilter == null)
            {
                resultFilter = _defaultFilter;
            }

            if (InternalLogger.IsTraceEnabled)
            {
                InternalLogger.Trace("Filter to apply: {0}", resultFilter);
            }

            // apply the condition to the buffer

            List <LogEventInfo> resultBuffer = new List <LogEventInfo>();

            for (int i = 0; i < logEvents.Length; ++i)
            {
                object v = resultFilter.Evaluate(logEvents[i]);
                if (v is bool && (bool)v)
                {
                    resultBuffer.Add(logEvents[i]);
                }
            }

            if (InternalLogger.IsTraceEnabled)
            {
                InternalLogger.Trace("After filtering: {0} events", resultBuffer.Count);
            }

            if (resultBuffer.Count > 0)
            {
                WrappedTarget.Write(resultBuffer.ToArray());
            }
        }
Esempio n. 9
0
        /// <summary>
        /// Evaluates all filtering rules to find the first one that matches.
        /// The matching rule determines the filtering condition to be applied
        /// to all items in a buffer. If no condition matches, default filter
        /// is applied to the array of log events.
        /// </summary>
        /// <param name="logEvents">Array of log events to be post-filtered.</param>
        protected override void Write(IList <AsyncLogEventInfo> logEvents)
        {
            ConditionExpression resultFilter = null;

            InternalLogger.Trace("PostFilteringWrapper(Name={0}): Running on {1} events", Name, logEvents.Count);

            // evaluate all the rules to get the filtering condition
            for (int i = 0; i < logEvents.Count; ++i)
            {
                foreach (FilteringRule rule in Rules)
                {
                    object v = rule.Exists.Evaluate(logEvents[i].LogEvent);

                    if (boxedTrue.Equals(v))
                    {
                        InternalLogger.Trace("PostFilteringWrapper(Name={0}): Rule matched: {1}", Name, rule.Exists);

                        resultFilter = rule.Filter;
                        break;
                    }
                }

                if (resultFilter != null)
                {
                    break;
                }
            }

            if (resultFilter == null)
            {
                resultFilter = DefaultFilter;
            }

            if (resultFilter == null)
            {
                WrappedTarget.WriteAsyncLogEvents(logEvents);
            }
            else
            {
                InternalLogger.Trace("PostFilteringWrapper(Name={0}): Filter to apply: {1}", Name, resultFilter);

                // apply the condition to the buffer
                var resultBuffer = new List <AsyncLogEventInfo>();

                for (int i = 0; i < logEvents.Count; ++i)
                {
                    object v = resultFilter.Evaluate(logEvents[i].LogEvent);
                    if (boxedTrue.Equals(v))
                    {
                        resultBuffer.Add(logEvents[i]);
                    }
                    else
                    {
                        // anything not passed down will be notified about successful completion
                        logEvents[i].Continuation(null);
                    }
                }

                InternalLogger.Trace("PostFilteringWrapper(Name={0}): After filtering: {1} events.", Name, resultBuffer.Count);
                if (resultBuffer.Count > 0)
                {
                    InternalLogger.Trace("PostFilteringWrapper(Name={0}): Sending to {1}", Name, WrappedTarget);
                    WrappedTarget.WriteAsyncLogEvents(resultBuffer);
                }
            }
        }
Esempio n. 10
0
 public virtual void RulesetLoaded(Ruleset rules, ActorInfo ai)
 {
     EnabledByDefault = RequiresCondition != null?RequiresCondition.Evaluate(NoConditions) > 0 : true;
 }