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

Use MSBuild Logging APIs for Task output instead of StdOut #933

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Fix infinite loop
  • Loading branch information
baronfel committed Mar 1, 2025
commit 179a4f9c1f89c1bc11dc04fc03247d9bda6305e5
Original file line number Diff line number Diff line change
@@ -8,7 +8,6 @@
using Microsoft.Extensions.Logging;
using Microsoft.Sbom.Api;
using Microsoft.Sbom.Api.Config;
using Microsoft.Sbom.Api.Config.Extensions;
using Microsoft.Sbom.Api.Converters;
using Microsoft.Sbom.Api.Convertors;
using Microsoft.Sbom.Api.Entities.Output;
@@ -46,38 +45,11 @@ namespace Microsoft.Sbom.Extensions.DependencyInjection;

public static class ServiceCollectionExtensions
{
public static IServiceCollection AddSbomConfiguration(this IServiceCollection services, InputConfiguration inputConfiguration, LogEventLevel logLevel = LogEventLevel.Information)
{
ArgumentNullException.ThrowIfNull(inputConfiguration);
services
.AddSingleton(_ =>
{
inputConfiguration.ToConfiguration();
return inputConfiguration;
})
.AddSbomTool();
return services;
}

public static IServiceCollection AddSbomTool(this IServiceCollection services, ILogger? logger = null)
public static IServiceCollection AddSbomTool(this IServiceCollection services)
{
services
.AddSingleton<IConfiguration, Configuration>()
.AddSingleton(x =>
{
if (logger != null)
{
return logger;
}
else
{
var level = x.GetService<InputConfiguration>()?.Verbosity?.Value ?? LogEventLevel.Information;
var defaultLogger = CreateDefaultLogger(level);
Log.Logger = defaultLogger;
return defaultLogger;
}
})
.AddTransient(x => FileSystemUtilsProvider.CreateInstance(x.GetRequiredService<ILogger>()))
.AddTransient(x => FileSystemUtilsProvider.CreateInstance(CreateDefaultLogger()))
.AddTransient<IWorkflow<SbomParserBasedValidationWorkflow>, SbomParserBasedValidationWorkflow>()
.AddTransient<IWorkflow<SbomGenerationWorkflow>, SbomGenerationWorkflow>()
.AddTransient<IWorkflow<SbomRedactionWorkflow>, SbomRedactionWorkflow>()
@@ -210,7 +182,7 @@ public static IServiceCollection ConfigureLoggingProviders(this IServiceCollecti
return services;
}

private static ILogger CreateDefaultLogger(LogEventLevel logLevel = LogEventLevel.Information)
public static ILogger CreateDefaultLogger(LogEventLevel logLevel = LogEventLevel.Information)
{
return new RemapComponentDetectionErrorsToWarningsLogger(
new LoggerConfiguration()
3 changes: 2 additions & 1 deletion src/Microsoft.Sbom.Targets/GenerateSbomTask.cs
Original file line number Diff line number Diff line change
@@ -63,7 +63,8 @@ public override bool Execute()
services
.AddSingleton<IConfiguration, Configuration>()
.AddSingleton(runtimeConfiguration)
.AddSbomTool(msbuildLogger)
.AddSingleton<Serilog.ILogger>(msbuildLogger)
.AddSbomTool()
/* Manually adding some dependencies since `AddSbomTool()` does not add them when
* running the MSBuild Task from another project.
*/
2 changes: 1 addition & 1 deletion src/Microsoft.Sbom.Targets/MSBuildLogger.cs
Original file line number Diff line number Diff line change
@@ -29,7 +29,7 @@ public MSBuildLogger(TaskLoggingHelper loggingHelperToWrap)
public void Write(LogEvent logEvent)
{
var logLevel = logEvent.Level;
if (logLevel is LogEventLevel.Warning && logEvent.Properties["SourceContext"].ToString().StartsWith("Microsoft.ComponentDetection.Detectors", StringComparison.Ordinal))
if (logLevel is LogEventLevel.Warning && logEvent.Properties["SourceContext"].ToString().StartsWith("Microsoft.ComponentDetection", StringComparison.Ordinal))
{
// Component Detection fires a lot of warnings, translate these to Information
logLevel = LogEventLevel.Information;
8 changes: 7 additions & 1 deletion src/Microsoft.Sbom.Tool/Program.cs
Original file line number Diff line number Diff line change
@@ -76,7 +76,13 @@ await Host.CreateDefaultBuilder(args)
inputConfiguration.ToConfiguration();
return inputConfiguration;
})

.AddSingleton(x =>
{
var level = x.GetService<Common.Config.InputConfiguration>()?.Verbosity?.Value ?? Serilog.Events.LogEventLevel.Information;
var defaultLogger = Extensions.DependencyInjection.ServiceCollectionExtensions.CreateDefaultLogger(level);
Serilog.Log.Logger = defaultLogger;
return defaultLogger;
})
.AddSbomTool();
})
.RunConsoleAsync(x => x.SuppressStatusMessages = true);