diff --git a/src/Microsoft.FeatureManagement/FeatureFilters/PercentageFilter.cs b/src/Microsoft.FeatureManagement/FeatureFilters/PercentageFilter.cs index c7ea8840..36e6d5e1 100644 --- a/src/Microsoft.FeatureManagement/FeatureFilters/PercentageFilter.cs +++ b/src/Microsoft.FeatureManagement/FeatureFilters/PercentageFilter.cs @@ -21,9 +21,9 @@ public class PercentageFilter : IFeatureFilter, IFilterParametersBinder /// Creates a percentage based feature filter. /// /// A logger factory for creating loggers. - public PercentageFilter(ILoggerFactory loggerFactory) + public PercentageFilter(ILoggerFactory loggerFactory = null) { - _logger = loggerFactory.CreateLogger(); + _logger = loggerFactory?.CreateLogger(); } /// @@ -51,7 +51,10 @@ public Task EvaluateAsync(FeatureFilterEvaluationContext context) if (settings.Value < 0) { - _logger.LogWarning($"The '{Alias}' feature filter does not have a valid '{nameof(settings.Value)}' value for feature '{context.FeatureName}'"); + if (_logger != null) + { + _logger.LogWarning($"The '{Alias}' feature filter does not have a valid '{nameof(settings.Value)}' value for feature '{context.FeatureName}'"); + } result = false; } diff --git a/src/Microsoft.FeatureManagement/FeatureFilters/TimeWindowFilter.cs b/src/Microsoft.FeatureManagement/FeatureFilters/TimeWindowFilter.cs index 884332b3..6f6d49f4 100644 --- a/src/Microsoft.FeatureManagement/FeatureFilters/TimeWindowFilter.cs +++ b/src/Microsoft.FeatureManagement/FeatureFilters/TimeWindowFilter.cs @@ -21,9 +21,9 @@ public class TimeWindowFilter : IFeatureFilter, IFilterParametersBinder /// Creates a time window based feature filter. /// /// A logger factory for creating loggers. - public TimeWindowFilter(ILoggerFactory loggerFactory) + public TimeWindowFilter(ILoggerFactory loggerFactory = null) { - _logger = loggerFactory.CreateLogger(); + _logger = loggerFactory?.CreateLogger(); } /// @@ -51,7 +51,10 @@ public Task EvaluateAsync(FeatureFilterEvaluationContext context) if (!settings.Start.HasValue && !settings.End.HasValue) { - _logger.LogWarning($"The '{Alias}' feature filter is not valid for feature '{context.FeatureName}'. It must have have specify either '{nameof(settings.Start)}', '{nameof(settings.End)}', or both."); + if (_logger != null) + { + _logger.LogWarning($"The '{Alias}' feature filter is not valid for feature '{context.FeatureName}'. It must have have specify either '{nameof(settings.Start)}', '{nameof(settings.End)}', or both."); + } return Task.FromResult(false); } diff --git a/src/Microsoft.FeatureManagement/Targeting/ContextualTargetingFilter.cs b/src/Microsoft.FeatureManagement/Targeting/ContextualTargetingFilter.cs index d29cafcd..2310d380 100644 --- a/src/Microsoft.FeatureManagement/Targeting/ContextualTargetingFilter.cs +++ b/src/Microsoft.FeatureManagement/Targeting/ContextualTargetingFilter.cs @@ -28,10 +28,10 @@ public class ContextualTargetingFilter : IContextualFeatureFilter /// Options controlling the behavior of the targeting evaluation performed by the filter. /// A logger factory for creating loggers. - public ContextualTargetingFilter(IOptions options, ILoggerFactory loggerFactory) + public ContextualTargetingFilter(IOptions options = null, ILoggerFactory loggerFactory = null) { - _options = options?.Value ?? throw new ArgumentNullException(nameof(options)); - _logger = loggerFactory?.CreateLogger() ?? throw new ArgumentNullException(nameof(loggerFactory)); + _options = options?.Value ?? new TargetingEvaluationOptions(); + _logger = loggerFactory?.CreateLogger(); } private StringComparison ComparisonType => _options.IgnoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal; diff --git a/src/Microsoft.FeatureManagement/Targeting/TargetingFilter.cs b/src/Microsoft.FeatureManagement/Targeting/TargetingFilter.cs index d57e4b31..77575958 100644 --- a/src/Microsoft.FeatureManagement/Targeting/TargetingFilter.cs +++ b/src/Microsoft.FeatureManagement/Targeting/TargetingFilter.cs @@ -26,11 +26,11 @@ public class TargetingFilter : IFeatureFilter, IFilterParametersBinder /// Options controlling the behavior of the targeting evaluation performed by the filter. /// An accessor used to acquire the targeting context for use in feature evaluation. /// A logger factory for creating loggers. - public TargetingFilter(IOptions options, ITargetingContextAccessor contextAccessor, ILoggerFactory loggerFactory) + public TargetingFilter(ITargetingContextAccessor contextAccessor, IOptions options = null, ILoggerFactory loggerFactory = null) { _contextAccessor = contextAccessor ?? throw new ArgumentNullException(nameof(contextAccessor)); _contextualFilter = new ContextualTargetingFilter(options, loggerFactory); - _logger = loggerFactory?.CreateLogger() ?? throw new ArgumentNullException(nameof(loggerFactory)); + _logger = loggerFactory?.CreateLogger(); } /// @@ -64,7 +64,10 @@ public async Task EvaluateAsync(FeatureFilterEvaluationContext context) // Ensure targeting can be performed if (targetingContext == null) { - _logger.LogWarning("No targeting context available for targeting evaluation."); + if (_logger != null) + { + _logger.LogWarning("No targeting context available for targeting evaluation."); + } return false; }