diff --git a/serilog-filters-expressions.sln.DotSettings b/serilog-filters-expressions.sln.DotSettings new file mode 100644 index 0000000..2a78c14 --- /dev/null +++ b/serilog-filters-expressions.sln.DotSettings @@ -0,0 +1,4 @@ + + True + True + True \ No newline at end of file diff --git a/src/Serilog.Filters.Expressions/LoggerEnrichmentConfigurationExtensions.cs b/src/Serilog.Filters.Expressions/LoggerEnrichmentConfigurationExtensions.cs new file mode 100644 index 0000000..89e031b --- /dev/null +++ b/src/Serilog.Filters.Expressions/LoggerEnrichmentConfigurationExtensions.cs @@ -0,0 +1,48 @@ +// Copyright 2019 Serilog Contributors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +using System; +using Serilog.Configuration; +using Serilog.Filters.Expressions; + +namespace Serilog +{ + /// + /// Extends logger enrichment configuration with methods for filtering with expressions. + /// + public static class LoggerEnrichmentConfigurationExtensions + { + /// + /// Write to a sink only when evaluates to true. + /// + /// Enrichment configuration. + /// An expression that evaluates to true when the supplied + /// should be enriched. + /// An action that configures the wrapped enricher. + /// Configuration object allowing method chaining. + /// The underlying . + public static LoggerConfiguration When( + this LoggerEnrichmentConfiguration loggerEnrichmentConfiguration, + string expression, + Action 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); + } + } +} diff --git a/src/Serilog.Filters.Expressions/LoggerFilterConfigurationExtensions.cs b/src/Serilog.Filters.Expressions/LoggerFilterConfigurationExtensions.cs index c2efcc5..d8bb248 100644 --- a/src/Serilog.Filters.Expressions/LoggerFilterConfigurationExtensions.cs +++ b/src/Serilog.Filters.Expressions/LoggerFilterConfigurationExtensions.cs @@ -5,7 +5,7 @@ namespace Serilog { /// - /// Extends logger configuration with methods for filtering with expressions. + /// Extends logger filter configuration with methods for filtering with expressions. /// public static class LoggerFilterConfigurationExtensions { diff --git a/src/Serilog.Filters.Expressions/LoggerSinkConfigurationExtensions.cs b/src/Serilog.Filters.Expressions/LoggerSinkConfigurationExtensions.cs new file mode 100644 index 0000000..aeff57b --- /dev/null +++ b/src/Serilog.Filters.Expressions/LoggerSinkConfigurationExtensions.cs @@ -0,0 +1,49 @@ +// Copyright 2019 Serilog Contributors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +using System; +using Serilog.Configuration; +using Serilog.Filters.Expressions; + +namespace Serilog +{ + /// + /// Extends logger sink configuration with methods for filtering with expressions. + /// + public static class LoggerSinkConfigurationExtensions + { + /// + /// Write to a sink only when evaluates to true. + /// + /// Sink configuration. + /// An expression that evaluates to true when the + /// supplied + /// should be written to the configured sink. + /// An action that configures the wrapped sink. + /// Configuration object allowing method chaining. + /// The underlying . + public static LoggerConfiguration Conditional( + this LoggerSinkConfiguration loggerSinkConfiguration, + string expression, + Action 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 = FilterLanguage.CreateFilter(expression); + return loggerSinkConfiguration.Conditional(e => true.Equals(compiled(e)), configureSink); + } + } +} diff --git a/src/Serilog.Filters.Expressions/Serilog.Filters.Expressions.csproj b/src/Serilog.Filters.Expressions/Serilog.Filters.Expressions.csproj index 9a66b77..03b00a1 100644 --- a/src/Serilog.Filters.Expressions/Serilog.Filters.Expressions.csproj +++ b/src/Serilog.Filters.Expressions/Serilog.Filters.Expressions.csproj @@ -2,9 +2,9 @@ Expression-based event filtering for Serilog. - 2.0.1 + 2.1.0 Serilog Contributors - net4.5;netstandard1.5 + net4.5;netstandard1.5;netstandard2.0 true true Serilog.Filters.Expressions @@ -24,8 +24,8 @@ - - + + diff --git a/test/Serilog.Filters.Expressions.Tests/ConfigurationTests.cs b/test/Serilog.Filters.Expressions.Tests/ConfigurationTests.cs new file mode 100644 index 0000000..6ef3d48 --- /dev/null +++ b/test/Serilog.Filters.Expressions.Tests/ConfigurationTests.cs @@ -0,0 +1,38 @@ +using System.Linq; +using Serilog.Filters.Expressions.Tests.Support; +using Xunit; + +namespace Serilog.Filters.Expressions.Tests +{ + public class ConfigurationTests + { + [Fact] + public void ExpressionsControlConditionalSinks() + { + var sink = new CollectingSink(); + var logger = new LoggerConfiguration() + .WriteTo.Conditional("A = 1 or A = 2", wt => wt.Sink(sink)) + .CreateLogger(); + + foreach (var a in Enumerable.Range(0, 5)) + logger.Information("{A}", a); + + Assert.Equal(2, sink.Events.Count); + } + + [Fact] + public void ExpressionsControlConditionalEnrichment() + { + var sink = new CollectingSink(); + var logger = new LoggerConfiguration() + .Enrich.When("A = 1 or A = 2", e => e.WithProperty("B", 1)) + .WriteTo.Sink(sink) + .CreateLogger(); + + foreach (var a in Enumerable.Range(0, 5)) + logger.Information("{A}", a); + + Assert.Equal(2, sink.Events.Count(e => e.Properties.ContainsKey("B"))); + } + } +}