Esempio n. 1
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));
        }
Esempio n. 2
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>Configuration object allowing method chaining.</returns>
        /// <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 = FilterLanguage.CreateFilter(expression);

            return(loggerEnrichmentConfiguration.When(e => true.Equals(compiled(e)), configureEnricher));
        }