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> #352

Draft
wants to merge 1 commit 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
8 changes: 8 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -814,6 +814,10 @@ dotnet_diagnostic.CA1825.severity = suggestion
# CA1829: Use Length/Count property instead of Count() when available
dotnet_diagnostic.CA1829.severity = suggestion

# https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca1848
# CA1848: Use the LoggerMessage delegates
dotnet_diagnostic.CA1848.severity = suggestion

# https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca1849
# CA1849: '...' synchronously blocks. Await '...' instead.
dotnet_diagnostic.CA1849.severity = suggestion
Expand Down Expand Up @@ -886,6 +890,10 @@ dotnet_diagnostic.CA2231.severity = suggestion
# CA2235: Mark all non-serializable fields
dotnet_diagnostic.CA2235.severity = suggestion

# https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca2254
# CA2254: Template should be a static expression
dotnet_diagnostic.CA2254.severity = suggestion

# https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/compiler-messages/cs1570
# CS1570: XML comment has badly formed XML -- '...'
dotnet_diagnostic.CS1570.severity = suggestion
Expand Down
8 changes: 4 additions & 4 deletions src/Microsoft.Sbom.Api/Config/ConfigSanitizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ private ConfigurationSetting<string> GetNamespaceBaseUri(IConfiguration configur
};
}

// If the user provides the parameter even when the assembly attribute is provided,
// If the user provides the parameter even when the assembly attribute is provided,
// show a warning on the console.
if (!string.IsNullOrWhiteSpace(configuration.NamespaceUriBase?.Value))
{
Expand Down Expand Up @@ -223,8 +223,8 @@ private ConfigurationSetting<string> GetPackageSupplierFromAssembly(IConfigurati
return configuration.PackageSupplier;
}

return new ConfigurationSetting<string>
{
return new ConfigurationSetting<string>
{
Source = SettingSource.Default,
Value = assemblyConfig.DefaultPackageSupplier
};
Expand Down Expand Up @@ -261,4 +261,4 @@ private string EnsurePathEndsWithManifestFolderForGenerate(string value, Manifes

return value;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,19 @@
using Microsoft.Sbom.Contracts;
using Microsoft.Sbom.Contracts.Enums;
using Microsoft.Sbom.Extensions.Entities;
using Serilog;

namespace Microsoft.Sbom.Api.Converters;

using Microsoft.Extensions.Logging;

/// <summary>
/// Converts ScannedComponent objects of SbomComponent type to ExternalDocumentReferenceInfo.
/// </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 +44,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 Expand Up @@ -82,4 +83,4 @@ private ExternalDocumentReferenceInfo ConvertComponentToExternalReference(Scanne
DescribedElementID = sbomComponent.RootElementId
};
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,19 @@
using System.Threading.Tasks;
using Microsoft.Sbom.Api.Entities;
using Microsoft.Sbom.Extensions.Entities;
using Serilog;

namespace Microsoft.Sbom.Api.Converters;

using Microsoft.Extensions.Logging;

/// <summary>
/// Converts ExternalDocumentReferenceInfo objects to their path as string.
/// </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 +38,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 +54,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 All @@ -68,4 +69,4 @@ await errors.Writer.WriteAsync(new FileValidationResult

return (output, errors);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,17 @@
using Microsoft.Sbom.Extensions;
using Serilog.Events;
using Constants = Microsoft.Sbom.Api.Utils.Constants;
using ILogger = Serilog.ILogger;

namespace Microsoft.Sbom.Api.Executors;

using Microsoft.Extensions.Logging;

/// <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 @@ -35,7 +36,7 @@ public abstract class ComponentDetectionBaseWalker
private ComponentDetectionCliArgumentBuilder cliArgumentBuilder;

public ComponentDetectionBaseWalker(
ILogger log,
ILogger<ComponentDetectionBaseWalker> log,
ComponentDetectorCachedExecutor componentDetector,
IConfiguration configuration,
ISbomConfigProvider sbomConfigs,
Expand All @@ -44,15 +45,15 @@ public ComponentDetectionBaseWalker(
this.log = log ?? throw new ArgumentNullException(nameof(log));
this.componentDetector = componentDetector ?? throw new ArgumentNullException(nameof(componentDetector));
this.configuration = configuration ?? throw new ArgumentNullException(nameof(configuration));
this.sbomConfigs = sbomConfigs ?? throw new ArgumentNullException(nameof(sbomConfigs));
this.sbomConfigs = sbomConfigs ?? throw new ArgumentNullException(nameof(sbomConfigs));
this.fileSystemUtils = fileSystemUtils ?? throw new ArgumentNullException(nameof(fileSystemUtils));
}

public (ChannelReader<ScannedComponent> output, ChannelReader<ComponentDetectorException> error) GetComponents(string buildComponentDirPath)
{
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 @@ -121,7 +122,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 All @@ -136,4 +137,4 @@ async Task Scan(string path)
}

protected abstract IEnumerable<ScannedComponent> FilterScannedComponents(ScanResult result);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,24 @@
using Microsoft.Sbom.Adapters.Report;
using Microsoft.Sbom.Api.Entities;
using Microsoft.Sbom.Contracts;
using Serilog;

namespace Microsoft.Sbom.Api.Executors;

using Microsoft.Extensions.Logging;

/// <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 @@ -55,7 +56,7 @@ async Task ConvertComponentToPackage(ScannedComponent scannedComponent, Channel<

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 @@ -65,7 +66,7 @@ async Task ConvertComponentToPackage(ScannedComponent scannedComponent, Channel<
}
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 All @@ -77,4 +78,4 @@ await errors.Writer.WriteAsync(new FileValidationResult

return (output, errors);
}
}
}
21 changes: 11 additions & 10 deletions src/Microsoft.Sbom.Api/Executors/DirectoryWalker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,22 @@
using Microsoft.Sbom.Api.Exceptions;
using Microsoft.Sbom.Common;
using Microsoft.Sbom.Common.Config;
using Serilog;

namespace Microsoft.Sbom.Api.Executors;

using Microsoft.Extensions.Logging;

/// <summary>
/// Given a directory path, walks the subtree and returns all the
/// Given a directory path, walks the subtree and returns all the
/// files in the directory.
/// </summary>
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 +38,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 +58,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 +70,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 All @@ -87,4 +88,4 @@ await errors.Writer.WriteAsync(new FileValidationResult

return (output, errors);
}
}
}
12 changes: 7 additions & 5 deletions src/Microsoft.Sbom.Api/Executors/EnumeratorChannel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,20 @@
using System.Threading.Channels;
using System.Threading.Tasks;
using Microsoft.Sbom.Api.Entities;
using Serilog;

namespace Microsoft.Sbom.Api.Executors;

using Microsoft.Extensions.Logging;
using ILogger = Serilog.ILogger;

/// <summary>
/// A executor that enumerates over objects in an enumerator.
/// </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 +46,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 All @@ -61,4 +63,4 @@ await errors.Writer.WriteAsync(new FileValidationResult

return (output, errors);
}
}
}
Loading