Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Startup configuration options #26

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
13 changes: 13 additions & 0 deletions src/AspNetCore.VersionInfo/Configuration/ExclusionSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AspNetCore.VersionInfo.Configuration
{
public class ExclusionSettings
{
public IEnumerable<string> Keys { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AspNetCore.VersionInfo.Configuration
{
public class ExclusionSettingsService : IExclusionSettings
{
public IEnumerable<string> keys { get; set; }
public ExclusionSettingsService(List<String> keys)
{
this.keys = keys;
}
}
}
13 changes: 13 additions & 0 deletions src/AspNetCore.VersionInfo/Configuration/IExclusionSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AspNetCore.VersionInfo.Configuration
{
public interface IExclusionSettings
{
IEnumerable<string> keys { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,36 @@
using AspNetCore.VersionInfo.Providers;
using System;
using System.Collections.Generic;
using AspNetCore.VersionInfo.Configuration;
using AspNetCore.VersionInfo.Providers;
using Microsoft.Extensions.DependencyInjection;

namespace AspNetCore.VersionInfo
{
public static class VersionInfoBuilderExtensions
{
public static VersionInfoBuilder With<T>(this VersionInfoBuilder builder) where T : class, IInfoProvider
private static List<String> keyValues = new List<String>();
public static VersionInfoBuilder With<T>(this VersionInfoBuilder builder, Action<ExclusionSettings> configureOptions = null) where T : class, IInfoProvider
Copy link
Owner

Choose a reason for hiding this comment

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

In this way configureOptions is specific for the provider configuration, but all (excluded) keys of each provider are merged in a single array (static).

Copy link
Owner

Choose a reason for hiding this comment

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

I think a better approach is using AspNet Options framework... as example you could inspect AspnetCore.Healthchecks code for it

https://github.com/dotnet/aspnetcore/tree/main/src/HealthChecks/HealthChecks/src/DependencyInjection

{

if (configureOptions != null)
{
var settings = new ExclusionSettings();

configureOptions(settings);

foreach (var keyItem in settings.Keys)
{
keyValues.Add(keyItem);
}
}

builder.Services.AddTransient<IExclusionSettings>(serviceProvider =>
{

return new ExclusionSettingsService(keyValues);

});

builder.Services.AddTransient<IInfoProvider, T>();
return builder;
}
Expand Down
30 changes: 24 additions & 6 deletions src/AspNetCore.VersionInfo/Services/FlatInfoCollector.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Collections.Generic;
using System.Linq;
using AspNetCore.VersionInfo.Configuration;
using AspNetCore.VersionInfo.Models;
using AspNetCore.VersionInfo.Models.Collectors;
using AspNetCore.VersionInfo.Providers;
Expand All @@ -10,16 +12,18 @@ internal partial class FlatInfoCollector : IInfoCollector
{
private readonly IEnumerable<IInfoProvider> _infoHandlers;
private readonly ILogger<FlatInfoCollector> _logger;
private readonly IExclusionSettings _exclusionSettings;

#region LoggerMessage
[LoggerMessage(Level = LogLevel.Debug, Message = "Elaborating {handlerName} provider")]
private partial void LogElaboratingHandler(string handlerName);
#endregion

public FlatInfoCollector(IEnumerable<IInfoProvider> infoHandlers, ILogger<FlatInfoCollector> logger)
public FlatInfoCollector(IEnumerable<IInfoProvider> infoHandlers, ILogger<FlatInfoCollector> logger, IExclusionSettings exclusionSettings)
{
_infoHandlers = infoHandlers;
_logger = logger;
_exclusionSettings = exclusionSettings;
}
public ICollectorResult AggregateData()
{
Expand All @@ -30,12 +34,26 @@ public ICollectorResult AggregateData()
LogElaboratingHandler(handler.Name);
foreach (var d in handler.GetData())
{
result.Add(new VersionDataProviderKeyValueResult()
if (_exclusionSettings.keys.Count() == 0)
{
Key = d.Key,
Value = d.Value,
ProviderName = handler.Name
});
result.Add(new VersionDataProviderKeyValueResult()
{
Key = d.Key,
Value = d.Value,
ProviderName = handler.Name
});
}

else if (!_exclusionSettings.keys.Contains(d.Key))
{
result.Add(new VersionDataProviderKeyValueResult()
{
Key = d.Key,
Value = d.Value,
ProviderName = handler.Name
});

}
}
}

Expand Down