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

refactor: replace Serilog.ILogger with Microsoft.Extensions.Logging.ILogger<T> #467

Draft
wants to merge 9 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
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
10 changes: 10 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -464,11 +464,21 @@ stylecop.documentation.xmlHeader = false
# https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/SA1200.md
dotnet_diagnostic.SA1623.severity = none

# CA1848: Use the LoggerMessage delegates
# Justification: This rule currently throws an exception for our codebase.
# https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca1848
dotnet_diagnostic.CA1848.severity = none

# CA1859: Use concrete types when possible
# Justification: This rule currently throws an exception for our codebase.
# https://github.com/dotnet/roslyn-analyzers/issues/6852
dotnet_diagnostic.CA1859.severity = none

# CA2254: Template should be a static expression
# Justification: This rule currently throws an exception for our codebase.
# https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca2254
dotnet_diagnostic.CA2254.severity = none

# NU5104: A stable release of a package should not have a prerelease dependency
# Justification: The Mono.Unix package that allows our tool to be built on ARM is labeled as 7.1.0-final.1.21458.1 which is detected as a prerelease dependency.
# https://learn.microsoft.com/en-us/nuget/reference/errors-and-warnings/nu5104
Expand Down
1 change: 1 addition & 0 deletions src/Microsoft.Sbom.Api/Config/ConfigPostProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.IO;
using System.Linq;
using AutoMapper;
using Microsoft.Extensions.Logging;
using Microsoft.Sbom.Api.Output.Telemetry;
using Microsoft.Sbom.Common;
using Microsoft.Sbom.Common.Config;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
using System.Threading.Tasks;
using Microsoft.ComponentDetection.Contracts.BcdeModels;
using Microsoft.ComponentDetection.Contracts.TypedComponent;
using Microsoft.Extensions.Logging;
using Microsoft.Sbom.Api.Entities;
using Microsoft.Sbom.Contracts;
using Microsoft.Sbom.Contracts.Enums;
using Microsoft.Sbom.Extensions.Entities;
using Serilog;

namespace Microsoft.Sbom.Api.Converters;

Expand All @@ -20,9 +20,9 @@ namespace Microsoft.Sbom.Api.Converters;
/// </summary>
public class ComponentToExternalReferenceInfoConverter
{
private readonly ILogger log;
private readonly ILogger<ComponentToExternalReferenceInfoConverter> log;

public ComponentToExternalReferenceInfoConverter(ILogger log)
public ComponentToExternalReferenceInfoConverter(ILogger<ComponentToExternalReferenceInfoConverter> log)
{
this.log = log ?? throw new ArgumentNullException(nameof(log));
}
Expand All @@ -43,7 +43,7 @@ public ComponentToExternalReferenceInfoConverter(ILogger log)
}
catch (Exception e)
{
log.Debug($"Encountered an error while converting SBOM component {scannedComponent.Component.Id} to external reference: {e.Message}");
log.LogDebug($"Encountered an error while converting SBOM component {scannedComponent.Component.Id} to external reference: {e.Message}");
await errors.Writer.WriteAsync(new FileValidationResult
{
ErrorType = Entities.ErrorType.PackageError,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
using System;
using System.Threading.Channels;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Microsoft.Sbom.Api.Entities;
using Microsoft.Sbom.Extensions.Entities;
using Serilog;

namespace Microsoft.Sbom.Api.Converters;

Expand All @@ -15,9 +15,9 @@ namespace Microsoft.Sbom.Api.Converters;
/// </summary>
public class ExternalReferenceInfoToPathConverter
{
private readonly ILogger log;
private readonly ILogger<ExternalReferenceInfoToPathConverter> log;

public ExternalReferenceInfoToPathConverter(ILogger log)
public ExternalReferenceInfoToPathConverter(ILogger<ExternalReferenceInfoToPathConverter> log)
{
this.log = log ?? throw new ArgumentNullException(nameof(log));
}
Expand All @@ -37,7 +37,7 @@ public ExternalReferenceInfoToPathConverter(ILogger log)

if (path == null)
{
log.Debug($"Encountered an error while converting external reference {externalDocumentRef.ExternalDocumentName} for null path.");
log.LogDebug($"Encountered an error while converting external reference {externalDocumentRef.ExternalDocumentName} for null path.");
await errors.Writer.WriteAsync(new FileValidationResult
{
ErrorType = ErrorType.Other,
Expand All @@ -53,7 +53,7 @@ await errors.Writer.WriteAsync(new FileValidationResult
}
catch (Exception e)
{
log.Debug($"Encountered an error while converting external reference {externalDocumentRef.ExternalDocumentName} to path: {e.Message}");
log.LogDebug($"Encountered an error while converting external reference {externalDocumentRef.ExternalDocumentName} to path: {e.Message}");
await errors.Writer.WriteAsync(new FileValidationResult
{
ErrorType = ErrorType.Other,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
using System.Threading.Tasks;
using Microsoft.ComponentDetection.Contracts;
using Microsoft.ComponentDetection.Contracts.BcdeModels;
using Microsoft.Sbom.Adapters.ComponentDetection;
using Microsoft.Sbom.Api.Config.Extensions;
using Microsoft.Sbom.Api.Exceptions;
using Microsoft.Sbom.Api.PackageDetails;
Expand All @@ -19,16 +18,18 @@
using Microsoft.Sbom.Common.Config;
using Microsoft.Sbom.Extensions;
using Constants = Microsoft.Sbom.Api.Utils.Constants;
using ILogger = Serilog.ILogger;

namespace Microsoft.Sbom.Api.Executors;

using Microsoft.Extensions.Logging;
using Microsoft.Sbom.Adapters.ComponentDetection;

/// <summary>
/// Abstract class that runs component detection tool in the given folder.
/// </summary>
public abstract class ComponentDetectionBaseWalker
{
private readonly ILogger log;
private readonly ILogger<ComponentDetectionBaseWalker> log;
private readonly ComponentDetectorCachedExecutor componentDetector;
private readonly IConfiguration configuration;
private readonly ISbomConfigProvider sbomConfigs;
Expand All @@ -42,7 +43,7 @@ public abstract class ComponentDetectionBaseWalker
private ComponentDetectionCliArgumentBuilder cliArgumentBuilder;

public ComponentDetectionBaseWalker(
ILogger log,
ILogger<ComponentDetectionBaseWalker> log,
ComponentDetectorCachedExecutor componentDetector,
IConfiguration configuration,
ISbomConfigProvider sbomConfigs,
Expand All @@ -63,7 +64,7 @@ public ComponentDetectionBaseWalker(
{
if (fileSystemUtils.FileExists(buildComponentDirPath))
{
log.Debug($"Scanning for packages under the root path {buildComponentDirPath}.");
log.LogDebug($"Scanning for packages under the root path {buildComponentDirPath}.");
}

// If the buildComponentDirPath is null or empty, make sure we have a ManifestDirPath and create a new temp directory with a random name.
Expand Down Expand Up @@ -151,7 +152,7 @@ async Task Scan(string path)

LicenseDictionary = licenseInformationFetcher.GetLicenseDictionary();

log.Information($"Found license information for {LicenseDictionary.Count} out of {uniqueComponents.Count()} unique components.");
log.LogInformation($"Found license information for {LicenseDictionary.Count} out of {uniqueComponents.Count()} unique components.");
}
}

Expand Down Expand Up @@ -196,7 +197,7 @@ async Task Scan(string path)
}
catch (Exception e)
{
log.Error($"Unknown error while running CD scan: {e}");
log.LogError($"Unknown error while running CD scan: {e}");
await errors.Writer.WriteAsync(new ComponentDetectorException("Unknown exception", e));
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,27 @@
using System.Threading.Channels;
using System.Threading.Tasks;
using Microsoft.ComponentDetection.Contracts.BcdeModels;
using Microsoft.Extensions.Logging;
using Microsoft.Sbom.Adapters.ComponentDetection;
using Microsoft.Sbom.Adapters.Report;
using Microsoft.Sbom.Api.Entities;
using Microsoft.Sbom.Contracts;
using Serilog;

namespace Microsoft.Sbom.Api.Executors;

using Microsoft.Sbom.Adapters.ComponentDetection;

/// <summary>
/// Takes a <see cref="ScannedComponent"/> object and converts it to a <see cref="PackageInfo"/>
/// object using a <see cref="IPackageInfoConverter"/>.
/// </summary>
public class ComponentToPackageInfoConverter
{
private readonly ILogger log;
private readonly ILogger<ComponentToPackageInfoConverter> log;

// TODO: Remove and use interface
// For unit testing only
public ComponentToPackageInfoConverter() { }

public ComponentToPackageInfoConverter(ILogger log)
public ComponentToPackageInfoConverter(ILogger<ComponentToPackageInfoConverter> log)
{
this.log = log ?? throw new ArgumentNullException(nameof(log));
}
Expand Down Expand Up @@ -56,7 +55,7 @@ async Task ConvertComponentToPackage(ExtendedScannedComponent scannedComponent,

if (sbom == null)
{
log.Debug($"Unable to serialize component '{scannedComponent.Component.Id}' of type '{scannedComponent.DetectorId}'. " +
log.LogDebug($"Unable to serialize component '{scannedComponent.Component.Id}' of type '{scannedComponent.DetectorId}'. " +
$"This component won't be included in the generated SBOM.");
}
else
Expand All @@ -66,7 +65,7 @@ async Task ConvertComponentToPackage(ExtendedScannedComponent scannedComponent,
}
catch (Exception e)
{
log.Debug($"Encountered an error while processing package {scannedComponent.Component.Id}: {e.Message}");
log.LogDebug($"Encountered an error while processing package {scannedComponent.Component.Id}: {e.Message}");
await errors.Writer.WriteAsync(new FileValidationResult
{
ErrorType = ErrorType.PackageError,
Expand Down
16 changes: 8 additions & 8 deletions src/Microsoft.Sbom.Api/Executors/DirectoryWalker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
using System.Linq;
using System.Threading.Channels;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Microsoft.Sbom.Api.Entities;
using Microsoft.Sbom.Api.Exceptions;
using Microsoft.Sbom.Common;
using Microsoft.Sbom.Common.Config;
using Serilog;

namespace Microsoft.Sbom.Api.Executors;

Expand All @@ -20,10 +20,10 @@ namespace Microsoft.Sbom.Api.Executors;
public class DirectoryWalker
{
private readonly IFileSystemUtils fileSystemUtils;
private readonly ILogger log;
private readonly ILogger<DirectoryWalker> log;
private readonly bool followSymlinks;

public DirectoryWalker(IFileSystemUtils fileSystemUtils, ILogger log, IConfiguration configuration)
public DirectoryWalker(IFileSystemUtils fileSystemUtils, ILogger<DirectoryWalker> log, IConfiguration configuration)
{
if (configuration is null)
{
Expand All @@ -37,13 +37,13 @@ public DirectoryWalker(IFileSystemUtils fileSystemUtils, ILogger log, IConfigura

if (!followSymlinks)
{
log.Information("FollowSymlinks parameter is set to false, we won't follow symbolic links while traversing the filesystem.");
log.LogInformation("FollowSymlinks parameter is set to false, we won't follow symbolic links while traversing the filesystem.");
}
}

public (ChannelReader<string> file, ChannelReader<FileValidationResult> errors) GetFilesRecursively(string root)
{
log.Debug($"Enumerating files under the root path {root}.");
log.LogDebug($"Enumerating files under the root path {root}.");

if (!fileSystemUtils.DirectoryExists(root))
{
Expand All @@ -57,10 +57,10 @@ async Task WalkDir(string path)
{
try
{
log.Verbose("Enumerating files under the directory {path}", path);
log.LogTrace("Enumerating files under the directory {Path}", path);
foreach (var file in fileSystemUtils.GetFilesInDirectory(path, followSymlinks))
{
log.Verbose("Found file {file}.", file);
log.LogTrace("Found file {File}.", file);
await output.Writer.WriteAsync(file);
}

Expand All @@ -69,7 +69,7 @@ async Task WalkDir(string path)
}
catch (Exception e)
{
log.Debug($"Encountered an unknown error for {path}: {e.Message}");
log.LogDebug($"Encountered an unknown error for {path}: {e.Message}");
await errors.Writer.WriteAsync(new FileValidationResult
{
ErrorType = ErrorType.Other,
Expand Down
8 changes: 4 additions & 4 deletions src/Microsoft.Sbom.Api/Executors/EnumeratorChannel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
using System.Collections.Generic;
using System.Threading.Channels;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Microsoft.Sbom.Api.Entities;
using Serilog;

namespace Microsoft.Sbom.Api.Executors;

Expand All @@ -15,9 +15,9 @@ namespace Microsoft.Sbom.Api.Executors;
/// </summary>
public class EnumeratorChannel
{
private readonly ILogger log;
private readonly ILogger<EnumeratorChannel> log;

public EnumeratorChannel(ILogger log)
public EnumeratorChannel(ILogger<EnumeratorChannel> log)
{
this.log = log ?? throw new ArgumentNullException(nameof(log));
}
Expand All @@ -44,7 +44,7 @@ async Task Enumerate()
}
catch (Exception e)
{
log.Debug($"Encountered an unknown error while enumerating: {e.Message}");
log.LogDebug($"Encountered an unknown error while enumerating: {e.Message}");
await errors.Writer.WriteAsync(new FileValidationResult
{
ErrorType = ErrorType.Other
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
using System.Collections.Generic;
using System.Threading.Channels;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Microsoft.Sbom.Api.Entities;
using Microsoft.Sbom.Api.Manifest;
using Microsoft.Sbom.Extensions;
using Microsoft.Sbom.Extensions.Entities;
using Serilog;

namespace Microsoft.Sbom.Api.Executors;

Expand All @@ -20,11 +20,11 @@ namespace Microsoft.Sbom.Api.Executors;
public class ExternalDocumentReferenceWriter
{
private readonly ManifestGeneratorProvider manifestGeneratorProvider;
private readonly ILogger log;
private readonly ILogger<ExternalDocumentReferenceWriter> log;

public ExternalDocumentReferenceWriter(
ManifestGeneratorProvider manifestGeneratorProvider,
ILogger log)
ILogger<ExternalDocumentReferenceWriter> log)
{
this.manifestGeneratorProvider = manifestGeneratorProvider ?? throw new ArgumentNullException(nameof(manifestGeneratorProvider));
this.log = log ?? throw new ArgumentNullException(nameof(log));
Expand Down Expand Up @@ -61,7 +61,7 @@ public ExternalDocumentReferenceWriter(
}
catch (Exception e)
{
log.Warning($"Encountered an error while generating json for external document reference {externalDocumentReferenceInfo.ExternalDocumentName}: {e.Message}");
log.LogWarning($"Encountered an error while generating json for external document reference {externalDocumentReferenceInfo.ExternalDocumentName}: {e.Message}");
await errors.Writer.WriteAsync(new FileValidationResult
{
ErrorType = ErrorType.JsonSerializationError,
Expand Down
8 changes: 4 additions & 4 deletions src/Microsoft.Sbom.Api/Executors/FileFilterer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
using System.Linq;
using System.Threading.Channels;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Microsoft.Sbom.Api.Entities;
using Microsoft.Sbom.Api.Filters;
using Microsoft.Sbom.Common;
using Microsoft.Sbom.Common.Config;
using Microsoft.Sbom.Extensions.Entities;
using Serilog;

namespace Microsoft.Sbom.Api.Executors;

Expand All @@ -21,13 +21,13 @@ namespace Microsoft.Sbom.Api.Executors;
public class FileFilterer
{
private readonly IFilter<DownloadedRootPathFilter> rootPathFilter;
private readonly ILogger log;
private readonly ILogger<FileFilterer> log;
private readonly IFileSystemUtils fileSystemUtils;
private readonly IConfiguration configuration;

public FileFilterer(
IFilter<DownloadedRootPathFilter> rootPathFilter,
ILogger log,
ILogger<FileFilterer> log,
IConfiguration configuration,
IFileSystemUtils fileSystemUtils)
{
Expand Down Expand Up @@ -96,7 +96,7 @@ await errors.Writer.WriteAsync(new FileValidationResult
}
catch (Exception e)
{
log.Debug($"Encountered an error while filtering file {file.Path}: {e.Message}");
log.LogDebug($"Encountered an error while filtering file {file.Path}: {e.Message}");
await errors.Writer.WriteAsync(new FileValidationResult
{
ErrorType = ErrorType.Other,
Expand Down
Loading