Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ public class PercentageFilter : IFeatureFilter, IFilterParametersBinder
/// Creates a percentage based feature filter.
/// </summary>
/// <param name="loggerFactory">A logger factory for creating loggers.</param>
public PercentageFilter(ILoggerFactory loggerFactory)
public PercentageFilter(ILoggerFactory loggerFactory = null)
{
_logger = loggerFactory.CreateLogger<PercentageFilter>();
_logger = loggerFactory?.CreateLogger<PercentageFilter>();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prior to this PR, before we have a default null value for loggerFactory, am I allowed to explicitly pass a null as parameter, i.e. new PercentageFilter(null)? If so, this PR also fixes the potential NPE, right?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes.

}

/// <summary>
Expand Down Expand Up @@ -51,7 +51,10 @@ public Task<bool> 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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ public class TimeWindowFilter : IFeatureFilter, IFilterParametersBinder
/// Creates a time window based feature filter.
/// </summary>
/// <param name="loggerFactory">A logger factory for creating loggers.</param>
public TimeWindowFilter(ILoggerFactory loggerFactory)
public TimeWindowFilter(ILoggerFactory loggerFactory = null)
{
_logger = loggerFactory.CreateLogger<TimeWindowFilter>();
_logger = loggerFactory?.CreateLogger<TimeWindowFilter>();
}

/// <summary>
Expand Down Expand Up @@ -51,7 +51,10 @@ public Task<bool> 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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ public class ContextualTargetingFilter : IContextualFeatureFilter<ITargetingCont
/// </summary>
/// <param name="options">Options controlling the behavior of the targeting evaluation performed by the filter.</param>
/// <param name="loggerFactory">A logger factory for creating loggers.</param>
public ContextualTargetingFilter(IOptions<TargetingEvaluationOptions> options, ILoggerFactory loggerFactory)
public ContextualTargetingFilter(IOptions<TargetingEvaluationOptions> options = null, ILoggerFactory loggerFactory = null)
{
_options = options?.Value ?? throw new ArgumentNullException(nameof(options));
_logger = loggerFactory?.CreateLogger<ContextualTargetingFilter>() ?? throw new ArgumentNullException(nameof(loggerFactory));
_options = options?.Value ?? new TargetingEvaluationOptions();
_logger = loggerFactory?.CreateLogger<ContextualTargetingFilter>();
}

private StringComparison ComparisonType => _options.IgnoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal;
Expand Down
9 changes: 6 additions & 3 deletions src/Microsoft.FeatureManagement/Targeting/TargetingFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ public class TargetingFilter : IFeatureFilter, IFilterParametersBinder
/// <param name="options">Options controlling the behavior of the targeting evaluation performed by the filter.</param>
/// <param name="contextAccessor">An accessor used to acquire the targeting context for use in feature evaluation.</param>
/// <param name="loggerFactory">A logger factory for creating loggers.</param>
public TargetingFilter(IOptions<TargetingEvaluationOptions> options, ITargetingContextAccessor contextAccessor, ILoggerFactory loggerFactory)
public TargetingFilter(ITargetingContextAccessor contextAccessor, IOptions<TargetingEvaluationOptions> options = null, ILoggerFactory loggerFactory = null)
{
_contextAccessor = contextAccessor ?? throw new ArgumentNullException(nameof(contextAccessor));
_contextualFilter = new ContextualTargetingFilter(options, loggerFactory);
_logger = loggerFactory?.CreateLogger<TargetingFilter>() ?? throw new ArgumentNullException(nameof(loggerFactory));
_logger = loggerFactory?.CreateLogger<TargetingFilter>();
}

/// <summary>
Expand Down Expand Up @@ -64,7 +64,10 @@ public async Task<bool> 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;
}
Expand Down