public void UserDefinedFunctionsAreCallableInExpressions()
        {
            var expr = SerilogExpression.Compile(
                "magic(10) + 3 = 55",
                new StaticMemberNameResolver(typeof(NameResolverTests)));

            Assert.True(Coerce.IsTrue(expr(Some.InformationEvent())));
        }
        public static LogEventPropertyValue?Magic(LogEventPropertyValue?number)
        {
            if (!Coerce.Numeric(number, out var num))
            {
                return(null);
            }

            return(new ScalarValue(num + 42));
        }
        /// <summary>
        /// Exclude log events that match the provided expression.
        /// </summary>
        /// <param name="loggerFilterConfiguration">Filter configuration.</param>
        /// <param name="expression">The expression to apply.</param>
        /// <returns>The underlying <see cref="LoggerConfiguration"/>.</returns>
        public static LoggerConfiguration ByExcluding(this LoggerFilterConfiguration loggerFilterConfiguration, string expression)
        {
            if (loggerFilterConfiguration == null)
            {
                throw new ArgumentNullException(nameof(loggerFilterConfiguration));
            }
            if (expression == null)
            {
                throw new ArgumentNullException(nameof(expression));
            }

            var compiled = SerilogExpression.Compile(expression);

            return(loggerFilterConfiguration.ByExcluding(e => Coerce.IsTrue(compiled(e))));
        }
        /// <inheritdoc/>
        public bool IsEnabled(LogEvent logEvent)
        {
            if (logEvent == null)
            {
                throw new ArgumentNullException(nameof(logEvent));
            }

            var filter = _filter;

            if (filter == null)
            {
                return(true);
            }

            return(Coerce.IsTrue(filter.Item2(logEvent)));
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Write to a sink only when <paramref name="expression" /> evaluates to <c>true</c>.
        /// </summary>
        /// <param name="loggerSinkConfiguration">Sink configuration.</param>
        /// <param name="expression">An expression that evaluates to <c>true</c> when the
        /// supplied <see cref="T:Serilog.Events.LogEvent" />
        /// should be written to the configured sink.</param>
        /// <param name="configureSink">An action that configures the wrapped sink.</param>
        /// <returns>Configuration object allowing method chaining.</returns>
        /// <returns>The underlying <see cref="LoggerConfiguration"/>.</returns>
        public static LoggerConfiguration Conditional(
            this LoggerSinkConfiguration loggerSinkConfiguration,
            string expression,
            Action <LoggerSinkConfiguration> configureSink)
        {
            if (loggerSinkConfiguration == null)
            {
                throw new ArgumentNullException(nameof(loggerSinkConfiguration));
            }
            if (expression == null)
            {
                throw new ArgumentNullException(nameof(expression));
            }
            if (configureSink == null)
            {
                throw new ArgumentNullException(nameof(configureSink));
            }

            var compiled = SerilogExpression.Compile(expression);

            return(loggerSinkConfiguration.Conditional(e => Coerce.IsTrue(compiled(e)), configureSink));
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Write to a sink only when <paramref name="expression" /> evaluates to <c>true</c>.
        /// </summary>
        /// <param name="loggerEnrichmentConfiguration">Enrichment configuration.</param>
        /// <param name="expression">An expression that evaluates to <c>true</c> when the supplied
        /// <see cref="T:Serilog.Events.LogEvent" /> should be enriched.</param>
        /// <param name="configureEnricher">An action that configures the wrapped enricher.</param>
        /// <returns>The underlying <see cref="LoggerConfiguration"/>.</returns>
        public static LoggerConfiguration When(
            this LoggerEnrichmentConfiguration loggerEnrichmentConfiguration,
            string expression,
            Action <LoggerEnrichmentConfiguration> configureEnricher)
        {
            if (loggerEnrichmentConfiguration == null)
            {
                throw new ArgumentNullException(nameof(loggerEnrichmentConfiguration));
            }
            if (expression == null)
            {
                throw new ArgumentNullException(nameof(expression));
            }
            if (configureEnricher == null)
            {
                throw new ArgumentNullException(nameof(configureEnricher));
            }

            var compiled = SerilogExpression.Compile(expression);

            return(loggerEnrichmentConfiguration.When(e => Coerce.IsTrue(compiled(e)), configureEnricher));
        }
Ejemplo n.º 7
0
 /// <summary>
 /// Test whether <paramref name="value"/> is <c langword="true">true</c>.
 /// </summary>
 /// <param name="value">The value to test, which may be <c langword="null">null</c>
 /// if the result of evaluating an expression was undefined.</param>
 /// <returns>Returns <c langword="true">true</c> if and only if the
 /// <paramref name="value"/> is a scalar Boolean with the
 /// value <c langword="true">true</c>. Returns <c langword="false">false</c>, otherwise.</returns>
 public static bool IsTrue(LogEventPropertyValue?value)
 {
     return(Coerce.IsTrue(value));
 }