-
Notifications
You must be signed in to change notification settings - Fork 167
Use MSBuild Logging APIs for Task output instead of StdOut #933
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
base: main
Are you sure you want to change the base?
Changes from all commits
297885d
54fc2d2
d4c1f08
e11db02
4f0ff8d
437863a
39a580e
f7732c2
2f9d59e
065fb37
0874fd8
29ba98c
d4442d4
05b7d47
745fe5b
1290105
a70ff15
0f400f5
9644f28
356fef7
b2bd375
3a788ad
ce545c8
ba73bea
68f73bb
558380d
c1332fd
72feabd
17fb04e
9b87d43
8763126
755e91d
7034219
6ade344
5eed0aa
848b13c
4b90c4a
264b289
9ef7330
7ba11f2
cb77851
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,7 +9,6 @@ | |
<ItemGroup> | ||
<PackageReference Include="Serilog" /> | ||
<PackageReference Include="Serilog.Sinks.Console" /> | ||
<PackageReference Include="System.IO.FileSystem.AccessControl" /> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is the reason for the change in this file? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as previous |
||
<PackageReference Include="System.Private.Uri" /> | ||
</ItemGroup> | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,6 +46,14 @@ namespace Microsoft.Sbom.Extensions.DependencyInjection; | |
|
||
public static class ServiceCollectionExtensions | ||
{ | ||
/// <summary> | ||
/// Initializes the services required to use the SBOM Tool using a default <see cref="InputConfiguration"/> and <see cref="LogEventLevel"/>. | ||
/// </summary> | ||
/// <param name="services">The services to which registrations are added.</param> | ||
/// <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> | ||
/// <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> | ||
/// <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> | ||
/// <returns></returns> | ||
public static IServiceCollection AddSbomConfiguration(this IServiceCollection services, InputConfiguration inputConfiguration, LogEventLevel logLevel = LogEventLevel.Information) | ||
{ | ||
ArgumentNullException.ThrowIfNull(inputConfiguration); | ||
|
@@ -59,16 +67,17 @@ public static IServiceCollection AddSbomConfiguration(this IServiceCollection se | |
return services; | ||
} | ||
|
||
/// <summary> | ||
/// Initializes the services required to use the SBOM Tool using a default <see cref="LogEventLevel"/>. | ||
/// </summary> | ||
/// <param name="services">The services to which registrations are added.</param> | ||
/// <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> | ||
/// <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> | ||
public static IServiceCollection AddSbomTool(this IServiceCollection services, LogEventLevel logLevel = LogEventLevel.Information) | ||
{ | ||
services | ||
.AddSingleton<IConfiguration, Configuration>() | ||
.AddTransient(_ => FileSystemUtilsProvider.CreateInstance(CreateLogger(logLevel))) | ||
.AddTransient(x => | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My understanding is that this will create a breaking change for API users, as they will now have to inject a logger similarly to how we are now doing it in Program.cs, is that right? This is okay with me as our next release will be a breaking change anyways, but tagging @DaveTryon for visibility There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oof, good call - I didn't realize that the dependency injection project was public API surface area. Let me noodle on how that could be fixed. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've pushed a commit that minimizes the API surface area changes, and adds a ton of xmldoc that instructs users on the new requirement of registering their own There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Forgind is there a way we can avoid making a breaking change here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. baronfel didn't come up with a way, and he knows this part much better than I do. I can try to look around, but I have little hope of success. |
||
{ | ||
logLevel = x.GetService<InputConfiguration>()?.Verbosity?.Value ?? logLevel; | ||
return Log.Logger = CreateLogger(logLevel); | ||
}) | ||
.AddTransient(x => FileSystemUtilsProvider.CreateInstance(CreateLogger(logLevel))) | ||
.AddTransient<IWorkflow<SbomParserBasedValidationWorkflow>, SbomParserBasedValidationWorkflow>() | ||
.AddTransient<IWorkflow<SbomGenerationWorkflow>, SbomGenerationWorkflow>() | ||
.AddTransient<IWorkflow<SbomConsolidationWorkflow>, SbomConsolidationWorkflow>() | ||
|
@@ -126,7 +135,8 @@ public static IServiceCollection AddSbomTool(this IServiceCollection services, L | |
.AddSingleton<IFileTypeUtils, FileTypeUtils>() | ||
.AddSingleton<ISignValidationProvider, SignValidationProvider>() | ||
.AddSingleton<IManifestParserProvider, ManifestParserProvider>() | ||
.AddSingleton(x => { | ||
.AddSingleton(x => | ||
{ | ||
var comparer = x.GetRequiredService<IOSUtils>().GetFileSystemStringComparer(); | ||
return new FileHashesDictionary(new ConcurrentDictionary<string, FileHashes>(comparer)); | ||
}) | ||
|
@@ -181,6 +191,11 @@ public static IServiceCollection AddSbomTool(this IServiceCollection services, L | |
return services; | ||
} | ||
|
||
/// <summary> | ||
/// This method integrates Serilog into the <see cref="Microsoft.Extensions.Logging"/> infrastructure. | ||
/// </summary> | ||
/// <param name="services"></param> | ||
/// <returns></returns> | ||
public static IServiceCollection ConfigureLoggingProviders(this IServiceCollection services) | ||
{ | ||
var providers = new LoggerProviderCollection(); | ||
|
@@ -201,7 +216,13 @@ public static IServiceCollection ConfigureLoggingProviders(this IServiceCollecti | |
return services; | ||
} | ||
|
||
private static ILogger CreateLogger(LogEventLevel logLevel) | ||
/// <summary> | ||
/// Creates a logger with the specified <paramref name="logLevel"/> that writes to a file and optionally to stderr. | ||
/// This logger converts all errors from ComponentDetection assemblies to warnings, and does not write tabular output to stdout/stderr, only the configured file. | ||
/// </summary> | ||
/// <param name="logLevel"></param> | ||
/// <returns></returns> | ||
public static ILogger CreateLogger(LogEventLevel logLevel = LogEventLevel.Information) | ||
{ | ||
return new RemapComponentDetectionErrorsToWarningsLogger( | ||
new LoggerConfiguration() | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the reason for the changes in this file?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When I built locally, I got an error that some package references might not be needed, and it pointed here. I deleted it, and it still built, so it seems to have been right.