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

feat: Switch to Serilog #441

Merged
merged 22 commits into from
Feb 23, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
073917c
refactor: initial switch to Serilog
melotic Feb 17, 2023
8636519
fix: create logger with a `LoggerProviderCollection`
melotic Feb 21, 2023
f2eb6c2
Merge remote-tracking branch 'origin/main' into justinperez/switch-to…
melotic Feb 21, 2023
c04ab19
fix: bad merge
melotic Feb 21, 2023
de07264
fix: update unit tests
melotic Feb 21, 2023
867ad76
fix: ILogger injection for PythonResolver.cs
melotic Feb 21, 2023
f62d6fc
fix: Add `Microsoft.Extensions.Logging` package to Verification Tests
melotic Feb 21, 2023
c975856
Merge branch 'main' into justinperez/switch-to-serilog
melotic Feb 21, 2023
40e7e8d
fix: only require MEL
melotic Feb 22, 2023
bf13015
fix: code review
melotic Feb 22, 2023
130c1a6
refactor: remove `ServiceBase`
melotic Feb 22, 2023
a8125f7
fix: remove serilog reference from more places
melotic Feb 22, 2023
6266e1f
Merge branch 'main' into justinperez/switch-to-serilog
melotic Feb 22, 2023
a99ce8d
fix: add reference to `System.Threading.Tasks.Dataflow` in verificati…
melotic Feb 22, 2023
a9fa372
fix: code review
melotic Feb 23, 2023
17f8f48
fix: setup logging in DI & proper file sink location
melotic Feb 23, 2023
97a13e1
Merge branch 'main' into justinperez/switch-to-serilog
melotic Feb 23, 2023
bba227d
refactor: switch `ConfigureLoggingProviders` to an extension method
melotic Feb 23, 2023
6d8b0a9
fix: reload bootstrap logger in Orchestrator.cs
melotic Feb 23, 2023
7bab7b1
fix: proper log file name
melotic Feb 23, 2023
1e7dfab
actually fix log file name 😋
melotic Feb 23, 2023
c8a04cb
fix: update verification test regex
melotic Feb 23, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
using Microsoft.ComponentDetection.Orchestrator.Services;
using Microsoft.ComponentDetection.Orchestrator.Services.GraphTranslation;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Serilog.Extensions.Logging;

public static class ServiceCollectionExtensions
{
Expand All @@ -37,6 +39,8 @@ public static IServiceCollection AddComponentDetection(this IServiceCollection s
{
services.AddSingleton<Orchestrator>();

ConfigureLoggingProviders(services);

// Shared services
services.AddSingleton<ITelemetryService, CommandLineTelemetryService>();
services.AddSingleton<ICommandLineInvocationService, CommandLineInvocationService>();
Expand Down Expand Up @@ -128,4 +132,23 @@ public static IServiceCollection AddComponentDetection(this IServiceCollection s

return services;
}

private static void ConfigureLoggingProviders(IServiceCollection services)
melotic marked this conversation as resolved.
Show resolved Hide resolved
{
var providers = new LoggerProviderCollection();
services.AddSingleton(providers);
services.AddSingleton<ILoggerFactory>(sc =>
{
var providerCollection = sc.GetService<LoggerProviderCollection>();
var factory = new SerilogLoggerFactory(null, true, providerCollection);

foreach (var provider in sc.GetServices<ILoggerProvider>())
{
factory.AddProvider(provider);
}

return factory;
});
services.AddLogging(l => l.AddFilter<SerilogLoggerProvider>(null, LogLevel.Trace));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
<PackageReference Include="Newtonsoft.Json" />
<PackageReference Include="Polly" />
<PackageReference Include="Serilog" />
melotic marked this conversation as resolved.
Show resolved Hide resolved
<PackageReference Include="Serilog.Extensions.Logging" />
<PackageReference Include="Serilog.Sinks.Console" />
<PackageReference Include="Serilog.Sinks.File" />
<PackageReference Include="System.Runtime.Loader" />
<PackageReference Include="System.Threading.Tasks.Dataflow" />
<PackageReference Include="System.Reactive" />
Expand Down
24 changes: 14 additions & 10 deletions src/Microsoft.ComponentDetection.Orchestrator/Orchestrator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ namespace Microsoft.ComponentDetection.Orchestrator;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
Expand All @@ -20,12 +21,11 @@ namespace Microsoft.ComponentDetection.Orchestrator;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Serilog.Core;
using Serilog;
using Serilog.Events;

public class Orchestrator
{
public static readonly LoggingLevelSwitch MinimumLogLevelSwitch = new();
private static readonly bool IsLinux = RuntimeInformation.IsOSPlatform(OSPlatform.Linux);

private readonly IServiceProvider serviceProvider;
Expand Down Expand Up @@ -59,14 +59,18 @@ public async Task<ScanResult> LoadAsync(string[] args, CancellationToken cancell
baseArguments = new BaseArguments();
}

// Set the minimum logging level
MinimumLogLevelSwitch.MinimumLevel = baseArguments.Verbosity switch
{
VerbosityMode.Quiet => LogEventLevel.Error,
VerbosityMode.Normal => LogEventLevel.Information,
VerbosityMode.Verbose => LogEventLevel.Debug,
_ => throw new ArgumentOutOfRangeException(nameof(baseArguments.Verbosity), "Invalid verbosity level"),
};
Log.Logger = new LoggerConfiguration()
.WriteTo.Console()
.WriteTo.File(Path.Combine(baseArguments.Output ?? Path.GetTempPath(), $"GovCompDiscLog_{DateTime.Now:yyyyMMddHHmmssfff}.txt"), buffered: true)
.MinimumLevel.Is(baseArguments.Verbosity switch
{
VerbosityMode.Quiet => LogEventLevel.Error,
VerbosityMode.Normal => LogEventLevel.Information,
VerbosityMode.Verbose => LogEventLevel.Debug,
_ => throw new ArgumentOutOfRangeException(nameof(baseArguments.Verbosity), "Invalid verbosity level"),
})
.Enrich.FromLogContext()
.CreateLogger();

// This is required so TelemetryRelay can be accessed via it's static singleton
// It should be refactored out at a later date
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@
<PackageReference Include="Microsoft.Extensions.Logging" />
<PackageReference Include="Serilog" />
<PackageReference Include="Serilog.Extensions.Hosting" />
<PackageReference Include="Serilog.Extensions.Logging" />
<PackageReference Include="Serilog.Sinks.Console" />
<PackageReference Include="Serilog.Sinks.File" />
<PackageReference Include="System.Threading.Tasks.Dataflow" />
</ItemGroup>

Expand Down
7 changes: 1 addition & 6 deletions src/Microsoft.ComponentDetection/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.ComponentDetection.Contracts;
Expand All @@ -22,14 +21,10 @@

Log.Logger = new LoggerConfiguration()
.WriteTo.Console()
.WriteTo.File(Path.Combine(Path.GetTempPath(), $"GovCompDiscLog_{DateTime.Now:yyyyMMddHHmmssfff}.txt"), buffered: true)
.MinimumLevel.ControlledBy(Orchestrator.MinimumLogLevelSwitch)
.Enrich.FromLogContext()
.CreateLogger();
.CreateBootstrapLogger();

var serviceProvider = new ServiceCollection()
.AddComponentDetection()
.AddLogging(l => l.AddSerilog(dispose: true))
.BuildServiceProvider();
var orchestrator = serviceProvider.GetRequiredService<Orchestrator>();
var result = await orchestrator.LoadAsync(args);
Expand Down