|
2 | 2 | // Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
3 | 3 |
|
4 | 4 | using System.Collections.Concurrent;
|
5 |
| -using Microsoft.Build.Utilities; |
6 | 5 | using Microsoft.ComponentDetection.Orchestrator;
|
7 | 6 | using Microsoft.ComponentDetection.Orchestrator.Extensions;
|
8 | 7 | using Microsoft.Extensions.DependencyInjection;
|
@@ -56,20 +55,29 @@ public static IServiceCollection AddSbomConfiguration(this IServiceCollection se
|
56 | 55 | inputConfiguration.ToConfiguration();
|
57 | 56 | return inputConfiguration;
|
58 | 57 | })
|
59 |
| - .AddSbomTool(logLevel); |
| 58 | + .AddSbomTool(); |
60 | 59 | return services;
|
61 | 60 | }
|
62 | 61 |
|
63 |
| - public static IServiceCollection AddSbomTool(this IServiceCollection services, LogEventLevel logLevel = LogEventLevel.Information, TaskLoggingHelper? taskLoggingHelper = null) |
| 62 | + public static IServiceCollection AddSbomTool(this IServiceCollection services, ILogger? logger = null) |
64 | 63 | {
|
65 | 64 | services
|
66 | 65 | .AddSingleton<IConfiguration, Configuration>()
|
67 |
| - .AddTransient(_ => FileSystemUtilsProvider.CreateInstance(CreateLogger(logLevel, taskLoggingHelper))) |
68 |
| - .AddTransient(x => |
| 66 | + .AddSingleton(x => |
69 | 67 | {
|
70 |
| - logLevel = x.GetService<InputConfiguration>()?.Verbosity?.Value ?? logLevel; |
71 |
| - return Log.Logger = CreateLogger(logLevel, taskLoggingHelper); |
| 68 | + if (logger != null) |
| 69 | + { |
| 70 | + return logger; |
| 71 | + } |
| 72 | + else |
| 73 | + { |
| 74 | + var level = x.GetService<InputConfiguration>()?.Verbosity?.Value ?? LogEventLevel.Information; |
| 75 | + var defaultLogger = CreateDefaultLogger(level); |
| 76 | + Log.Logger = defaultLogger; |
| 77 | + return defaultLogger; |
| 78 | + } |
72 | 79 | })
|
| 80 | + .AddTransient(x => FileSystemUtilsProvider.CreateInstance(x.GetRequiredService<ILogger>())) |
73 | 81 | .AddTransient<IWorkflow<SbomParserBasedValidationWorkflow>, SbomParserBasedValidationWorkflow>()
|
74 | 82 | .AddTransient<IWorkflow<SbomGenerationWorkflow>, SbomGenerationWorkflow>()
|
75 | 83 | .AddTransient<IWorkflow<SbomRedactionWorkflow>, SbomRedactionWorkflow>()
|
@@ -126,7 +134,8 @@ public static IServiceCollection AddSbomTool(this IServiceCollection services, L
|
126 | 134 | .AddSingleton<IFileTypeUtils, FileTypeUtils>()
|
127 | 135 | .AddSingleton<ISignValidationProvider, SignValidationProvider>()
|
128 | 136 | .AddSingleton<IManifestParserProvider, ManifestParserProvider>()
|
129 |
| - .AddSingleton(x => { |
| 137 | + .AddSingleton(x => |
| 138 | + { |
130 | 139 | var comparer = x.GetRequiredService<IOSUtils>().GetFileSystemStringComparer();
|
131 | 140 | return new FileHashesDictionary(new ConcurrentDictionary<string, FileHashes>(comparer));
|
132 | 141 | })
|
@@ -201,33 +210,26 @@ public static IServiceCollection ConfigureLoggingProviders(this IServiceCollecti
|
201 | 210 | return services;
|
202 | 211 | }
|
203 | 212 |
|
204 |
| - private static ILogger CreateLogger(LogEventLevel logLevel, TaskLoggingHelper? taskLoggingHelper = null) |
| 213 | + private static ILogger CreateDefaultLogger(LogEventLevel logLevel = LogEventLevel.Information) |
205 | 214 | {
|
206 |
| - if (taskLoggingHelper == null) |
207 |
| - { |
208 |
| - return new RemapComponentDetectionErrorsToWarningsLogger( |
209 |
| - new LoggerConfiguration() |
210 |
| - .MinimumLevel.ControlledBy(new LoggingLevelSwitch { MinimumLevel = logLevel }) |
211 |
| - .Filter.ByExcluding(Matching.FromSource("System.Net.Http.HttpClient")) |
212 |
| - .Enrich.With<LoggingEnricher>() |
213 |
| - .Enrich.FromLogContext() |
214 |
| - .WriteTo.Map( |
215 |
| - LoggingEnricher.LogFilePathPropertyName, |
216 |
| - (logFilePath, wt) => wt.Async(x => x.File($"{logFilePath}")), |
217 |
| - 1) // sinkMapCountLimit |
218 |
| - .WriteTo.Map<bool>( |
219 |
| - LoggingEnricher.PrintStderrPropertyName, |
220 |
| - (printLogsToStderr, wt) => wt.Logger(lc => lc |
221 |
| - .WriteTo.Console(outputTemplate: Constants.LoggerTemplate, standardErrorFromLevel: printLogsToStderr ? LogEventLevel.Debug : null) |
| 215 | + return new RemapComponentDetectionErrorsToWarningsLogger( |
| 216 | + new LoggerConfiguration() |
| 217 | + .MinimumLevel.ControlledBy(new LoggingLevelSwitch { MinimumLevel = logLevel }) |
| 218 | + .Filter.ByExcluding(Matching.FromSource("System.Net.Http.HttpClient")) |
| 219 | + .Enrich.With<LoggingEnricher>() |
| 220 | + .Enrich.FromLogContext() |
| 221 | + .WriteTo.Map( |
| 222 | + LoggingEnricher.LogFilePathPropertyName, |
| 223 | + (logFilePath, wt) => wt.Async(x => x.File($"{logFilePath}")), |
| 224 | + 1) // sinkMapCountLimit |
| 225 | + .WriteTo.Map<bool>( |
| 226 | + LoggingEnricher.PrintStderrPropertyName, |
| 227 | + (printLogsToStderr, wt) => wt.Logger(lc => lc |
| 228 | + .WriteTo.Console(outputTemplate: Constants.LoggerTemplate, standardErrorFromLevel: printLogsToStderr ? LogEventLevel.Debug : null) |
222 | 229 |
|
223 |
| - // Don't write the detection times table from DetectorProcessingService to the console, only the log file |
224 |
| - .Filter.ByExcluding(Matching.WithProperty<string>("DetectionTimeLine", x => !string.IsNullOrEmpty(x)))), |
225 |
| - 1) // sinkMapCountLimit |
226 |
| - .CreateLogger()); |
227 |
| - } |
228 |
| - else |
229 |
| - { |
230 |
| - return new MSBuildLogger(taskLoggingHelper); |
231 |
| - } |
| 230 | + // Don't write the detection times table from DetectorProcessingService to the console, only the log file |
| 231 | + .Filter.ByExcluding(Matching.WithProperty<string>("DetectionTimeLine", x => !string.IsNullOrEmpty(x)))), |
| 232 | + 1) // sinkMapCountLimit |
| 233 | + .CreateLogger()); |
232 | 234 | }
|
233 | 235 | }
|
0 commit comments