This repository has been archived by the owner on Feb 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #34 from nblumhardt/conditionals
Support for conditional sinks/enrichers
- Loading branch information
Showing
6 changed files
with
144 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation"> | ||
<s:Boolean x:Key="/Default/UserDictionary/Words/=Enricher/@EntryIndexedValue">True</s:Boolean> | ||
<s:Boolean x:Key="/Default/UserDictionary/Words/=nblumhardt/@EntryIndexedValue">True</s:Boolean> | ||
<s:Boolean x:Key="/Default/UserDictionary/Words/=Serilog/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary> |
48 changes: 48 additions & 0 deletions
48
src/Serilog.Filters.Expressions/LoggerEnrichmentConfigurationExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
{ | ||
/// <summary> | ||
/// Extends logger enrichment configuration with methods for filtering with expressions. | ||
/// </summary> | ||
public static class LoggerEnrichmentConfigurationExtensions | ||
{ | ||
/// <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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
src/Serilog.Filters.Expressions/LoggerSinkConfigurationExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
{ | ||
/// <summary> | ||
/// Extends logger sink configuration with methods for filtering with expressions. | ||
/// </summary> | ||
public static class LoggerSinkConfigurationExtensions | ||
{ | ||
/// <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 = FilterLanguage.CreateFilter(expression); | ||
return loggerSinkConfiguration.Conditional(e => true.Equals(compiled(e)), configureSink); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
test/Serilog.Filters.Expressions.Tests/ConfigurationTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"))); | ||
} | ||
} | ||
} |