Skip to content

Commit 649d0fc

Browse files
committed
Keep some of the previous API surface area to minimize breaks. Add documentation comments to clarify intended usage of members.
1 parent aa37566 commit 649d0fc

File tree

3 files changed

+47
-11
lines changed

3 files changed

+47
-11
lines changed

src/Microsoft.Sbom.Extensions.DependencyInjection/ServiceCollectionExtensions.cs

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using Microsoft.Extensions.Logging;
99
using Microsoft.Sbom.Api;
1010
using Microsoft.Sbom.Api.Config;
11+
using Microsoft.Sbom.Api.Config.Extensions;
1112
using Microsoft.Sbom.Api.Converters;
1213
using Microsoft.Sbom.Api.Convertors;
1314
using Microsoft.Sbom.Api.Entities.Output;
@@ -45,11 +46,38 @@ namespace Microsoft.Sbom.Extensions.DependencyInjection;
4546

4647
public static class ServiceCollectionExtensions
4748
{
48-
public static IServiceCollection AddSbomTool(this IServiceCollection services)
49+
/// <summary>
50+
/// Initializes the services required to use the SBOM Tool using a default <see cref="InputConfiguration"/> and <see cref="LogEventLevel"/>.
51+
/// </summary>
52+
/// <param name="services">The services to which registrations are added.</param>
53+
/// <param name="inputConfiguration">The default configuration to use, which will be registered as a singleton and will overwrite the currently-set <see cref="Configuration"/>.</param>
54+
/// <param name="logLevel">The log verbosity level with which to use for the minimal logger used for internal services. These services require an <see cref="ILogger"/> but may themselves be used to <strong>initialize</strong> an <see cref="ILogger"/>, so need separate configuration.</param>
55+
/// <remarks>The <see cref="InputConfiguration"/> is registered as a singleton in the container, and the <paramref name="logLevel"/> is used to create a default logger for internal utility classes, irrespective of any other <see cref="ILogger"/> registered in the container. This method does not register an instance of <see cref="ILogger"/>, however one is required for the system to function. Callers need to provide their own implementation.</remarks>
56+
/// <returns></returns>
57+
public static IServiceCollection AddSbomConfiguration(this IServiceCollection services, InputConfiguration inputConfiguration, LogEventLevel logLevel = LogEventLevel.Information)
58+
{
59+
ArgumentNullException.ThrowIfNull(inputConfiguration);
60+
services
61+
.AddSingleton(_ =>
62+
{
63+
inputConfiguration.ToConfiguration();
64+
return inputConfiguration;
65+
})
66+
.AddSbomTool(logLevel);
67+
return services;
68+
}
69+
70+
/// <summary>
71+
/// Initializes the services required to use the SBOM Tool using a default <see cref="LogEventLevel"/>.
72+
/// </summary>
73+
/// <param name="services">The services to which registrations are added.</param>
74+
/// <param name="logLevel">The log verbosity level with which to use for the minimal logger used for internal services. These services require an <see cref="ILogger"/> but may themselves be used to <strong>initialize</strong> an <see cref="ILogger"/>, so need separate configuration.</param>
75+
/// <remarks>This method does not register an instance of <see cref="ILogger"/>, however one is required for the system to function. Callers need to provide their own implementation.</remarks>
76+
public static IServiceCollection AddSbomTool(this IServiceCollection services, LogEventLevel logLevel = LogEventLevel.Information)
4977
{
5078
services
5179
.AddSingleton<IConfiguration, Configuration>()
52-
.AddTransient(x => FileSystemUtilsProvider.CreateInstance(CreateDefaultLogger()))
80+
.AddTransient(x => FileSystemUtilsProvider.CreateInstance(CreateLogger(logLevel)))
5381
.AddTransient<IWorkflow<SbomParserBasedValidationWorkflow>, SbomParserBasedValidationWorkflow>()
5482
.AddTransient<IWorkflow<SbomGenerationWorkflow>, SbomGenerationWorkflow>()
5583
.AddTransient<IWorkflow<SbomRedactionWorkflow>, SbomRedactionWorkflow>()
@@ -162,6 +190,11 @@ public static IServiceCollection AddSbomTool(this IServiceCollection services)
162190
return services;
163191
}
164192

193+
/// <summary>
194+
/// This method integrates Serilog into the <see cref="Microsoft.Extensions.Logging"/> infrastructure.
195+
/// </summary>
196+
/// <param name="services"></param>
197+
/// <returns></returns>
165198
public static IServiceCollection ConfigureLoggingProviders(this IServiceCollection services)
166199
{
167200
var providers = new LoggerProviderCollection();
@@ -182,7 +215,13 @@ public static IServiceCollection ConfigureLoggingProviders(this IServiceCollecti
182215
return services;
183216
}
184217

185-
public static ILogger CreateDefaultLogger(LogEventLevel logLevel = LogEventLevel.Information)
218+
/// <summary>
219+
/// Creates a logger with the specified <paramref name="logLevel"/> that writes to a file and optionally to stderr.
220+
/// This logger converts all errors from ComponentDetection assemblies to warnings, and does not write tabular output to stdout/stderr, only the configured file.
221+
/// </summary>
222+
/// <param name="logLevel"></param>
223+
/// <returns></returns>
224+
public static ILogger CreateLogger(LogEventLevel logLevel = LogEventLevel.Information)
186225
{
187226
return new RemapComponentDetectionErrorsToWarningsLogger(
188227
new LoggerConfiguration()

src/Microsoft.Sbom.Targets/GenerateSbomTask.cs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ namespace Microsoft.Sbom.Targets;
99
using Microsoft.Build.Framework;
1010
using Microsoft.Build.Utilities;
1111
using Microsoft.Extensions.DependencyInjection;
12-
using Microsoft.Extensions.Hosting;
1312
using Microsoft.Sbom.Api.Manifest.ManifestConfigHandlers;
1413
using Microsoft.Sbom.Api.Metadata;
1514
using Microsoft.Sbom.Api.Providers;
@@ -58,9 +57,7 @@ public override bool Execute()
5857
NoComponentGovernanceSummary = true
5958
};
6059

61-
var taskHost = Host.CreateDefaultBuilder()
62-
.ConfigureServices((host, services) =>
63-
services
60+
using var services = new ServiceCollection()
6461
.AddSingleton<IConfiguration, Configuration>()
6562
.AddSingleton(runtimeConfiguration)
6663
.AddSingleton<Serilog.ILogger>(msbuildLogger)
@@ -85,10 +82,10 @@ public override bool Execute()
8582
.AddSingleton<IManifestInterface, SPDX22.Validator>()
8683
.AddSingleton<IManifestInterface, SPDX30.Validator>()
8784
.AddSingleton<IManifestConfigHandler, SPDX22ManifestConfigHandler>()
88-
.AddSingleton<IManifestConfigHandler, SPDX30ManifestConfigHandler>())
89-
.Build();
85+
.AddSingleton<IManifestConfigHandler, SPDX30ManifestConfigHandler>()
86+
.BuildServiceProvider();
9087

91-
var generator = taskHost.Services.GetRequiredService<ISbomGenerator>();
88+
var generator = services.GetRequiredService<ISbomGenerator>();
9289

9390
// Set other configurations. The GenerateSBOMAsync() already sanitizes and checks for
9491
// a valid namespace URI and generates a random guid for NamespaceUriUniquePart if

src/Microsoft.Sbom.Tool/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ await Host.CreateDefaultBuilder(args)
7979
.AddSingleton(x =>
8080
{
8181
var level = x.GetService<Common.Config.InputConfiguration>()?.Verbosity?.Value ?? Serilog.Events.LogEventLevel.Information;
82-
var defaultLogger = Extensions.DependencyInjection.ServiceCollectionExtensions.CreateDefaultLogger(level);
82+
var defaultLogger = Extensions.DependencyInjection.ServiceCollectionExtensions.CreateLogger(level);
8383
Serilog.Log.Logger = defaultLogger;
8484
return defaultLogger;
8585
})

0 commit comments

Comments
 (0)