Skip to content

Commit

Permalink
feat: Switch to Serilog (#441)
Browse files Browse the repository at this point in the history
  • Loading branch information
melotic authored Feb 23, 2023
1 parent ca02614 commit 273d48e
Show file tree
Hide file tree
Showing 90 changed files with 651 additions and 904 deletions.
3 changes: 3 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -669,6 +669,9 @@ dotnet_diagnostic.CA1852.severity = suggestion
# CA1854: Prefer the IDictionary.TryGetValue(TKey, out TValue) method
dotnet_diagnostic.CA1854.severity = suggestion

# CA1848: Use the LoggerMessage delegates
dotnet_diagnostic.CA1848.severity = suggestion

# JSON002: Probable JSON string detected
dotnet_diagnostic.JSON002.severity = suggestion

Expand Down
88 changes: 46 additions & 42 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
@@ -1,44 +1,48 @@
<Project>

<ItemDefinitionGroup>
<PackageReference>
<!-- Do not share compile-time dependencies transitively. This requires that all projects reference all packages -->
<PrivateAssets>Compile</PrivateAssets>
</PackageReference>
</ItemDefinitionGroup>

<ItemGroup>
<PackageVersion Include="CommandLineParser" Version="2.9.1"/>
<PackageVersion Include="coverlet.msbuild" Version="3.2.0"/>
<PackageVersion Include="Docker.DotNet" Version="3.125.12"/>
<PackageVersion Include="FluentAssertions" Version="6.10.0"/>
<PackageVersion Include="Microsoft.AspNet.WebApi.Client" Version="5.2.9"/>
<PackageVersion Include="Microsoft.Extensions.Caching.Memory" Version="7.0.0"/>
<PackageVersion Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0"/>
<PackageVersion Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="7.0.0"/>
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.5.0"/>
<PackageVersion Include="Microsoft.SourceLink.GitHub" Version="1.1.1"/>
<PackageVersion Include="Microsoft.VisualStudio.Threading.Analyzers" Version="17.5.22"/>
<PackageVersion Include="DotNet.Glob" Version="2.1.1"/>
<PackageVersion Include="MinVer" Version="4.3.0"/>
<PackageVersion Include="Moq" Version="4.18.4"/>
<PackageVersion Include="morelinq" Version="3.3.2"/>
<PackageVersion Include="MSTest.TestAdapter" Version="3.0.2"/>
<PackageVersion Include="MSTest.TestFramework" Version="3.0.2"/>
<PackageVersion Include="Newtonsoft.Json" Version="13.0.2"/>
<PackageVersion Include="NuGet.ProjectModel" Version="6.5.0"/>
<PackageVersion Include="NuGet.Versioning" Version="6.5.0"/>
<PackageVersion Include="packageurl-dotnet" Version="1.0.0"/>
<PackageVersion Include="Polly" Version="7.2.3"/>
<PackageVersion Include="Semver" Version="2.3.0"/>
<PackageVersion Include="StyleCop.Analyzers" Version="1.2.0-beta.435"/>
<PackageVersion Include="System.Memory" Version="4.5.5"/>
<PackageVersion Include="System.Reactive" Version="5.0.0"/>
<PackageVersion Include="System.Runtime.Loader" Version="4.3.0"/>
<PackageVersion Include="System.Threading.Tasks.Dataflow" Version="7.0.0"/>
<PackageVersion Include="Tomlyn" Version="0.16.2"/>
<PackageVersion Include="yamldotnet" Version="13.0.1"/>
<PackageVersion Include="Faker.net" Version="2.0.154"/>
<PackageVersion Include="Valleysoft.DockerfileModel" Version="1.1.0"/>
</ItemGroup>
<ItemDefinitionGroup>
<PackageReference>
<!-- Do not share compile-time dependencies transitively. This requires that all projects reference all packages -->
<PrivateAssets>Compile</PrivateAssets>
</PackageReference>
</ItemDefinitionGroup>
<ItemGroup>
<PackageVersion Include="CommandLineParser" Version="2.9.1" />
<PackageVersion Include="coverlet.msbuild" Version="3.2.0" />
<PackageVersion Include="Docker.DotNet" Version="3.125.12" />
<PackageVersion Include="FluentAssertions" Version="6.10.0" />
<PackageVersion Include="Microsoft.AspNet.WebApi.Client" Version="5.2.9" />
<PackageVersion Include="Microsoft.Extensions.Caching.Memory" Version="7.0.0" />
<PackageVersion Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
<PackageVersion Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="7.0.0" />
<PackageVersion Include="Microsoft.Extensions.Logging" Version="7.0.0" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
<PackageVersion Include="Microsoft.SourceLink.GitHub" Version="1.1.1" />
<PackageVersion Include="Microsoft.VisualStudio.Threading.Analyzers" Version="17.5.22" />
<PackageVersion Include="DotNet.Glob" Version="2.1.1" />
<PackageVersion Include="MinVer" Version="4.3.0" />
<PackageVersion Include="Moq" Version="4.18.4" />
<PackageVersion Include="morelinq" Version="3.3.2" />
<PackageVersion Include="MSTest.TestAdapter" Version="3.0.2" />
<PackageVersion Include="MSTest.TestFramework" Version="3.0.2" />
<PackageVersion Include="Newtonsoft.Json" Version="13.0.2" />
<PackageVersion Include="NuGet.ProjectModel" Version="6.5.0" />
<PackageVersion Include="NuGet.Versioning" Version="6.5.0" />
<PackageVersion Include="packageurl-dotnet" Version="1.0.0" />
<PackageVersion Include="Polly" Version="7.2.3" />
<PackageVersion Include="Semver" Version="2.2.0" />
<PackageVersion Include="Serilog" Version="2.12.0" />
<PackageVersion Include="Serilog.Extensions.Hosting" Version="5.0.1" />
<PackageVersion Include="Serilog.Extensions.Logging" Version="3.1.0" />
<PackageVersion Include="Serilog.Sinks.Console" Version="4.1.0" />
<PackageVersion Include="Serilog.Sinks.File" Version="5.0.0" />
<PackageVersion Include="StyleCop.Analyzers" Version="1.2.0-beta.435" />
<PackageVersion Include="System.Memory" Version="4.5.5" />
<PackageVersion Include="System.Reactive" Version="5.0.0" />
<PackageVersion Include="System.Runtime.Loader" Version="4.3.0" />
<PackageVersion Include="System.Threading.Tasks.Dataflow" Version="7.0.0" />
<PackageVersion Include="Tomlyn" Version="0.16.2" />
<PackageVersion Include="yamldotnet" Version="13.0.1" />
<PackageVersion Include="Faker.net" Version="2.0.154" />
<PackageVersion Include="Valleysoft.DockerfileModel" Version="1.1.0" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -1,29 +1,31 @@
namespace Microsoft.ComponentDetection.Common;

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using Microsoft.ComponentDetection.Contracts;
using Microsoft.Extensions.Logging;

public class ComponentStreamEnumerable : IEnumerable<IComponentStream>
{
private readonly ILogger logger;

public ComponentStreamEnumerable(IEnumerable<MatchedFile> fileEnumerable, ILogger logger)
{
this.logger = logger;
this.ToEnumerate = fileEnumerable;
this.Logger = logger;
}

private IEnumerable<MatchedFile> ToEnumerate { get; }

private ILogger Logger { get; }

public IEnumerator<IComponentStream> GetEnumerator()
{
foreach (var filePairing in this.ToEnumerate)
{
if (!filePairing.File.Exists)
{
this.Logger.LogWarning($"File {filePairing.File.FullName} does not exist on disk.");
this.logger.LogWarning("File {FilePairingName} does not exist on disk.", filePairing.File.FullName);
yield break;
}

Expand Down Expand Up @@ -51,13 +53,12 @@ private Stream SafeOpenFile(FileInfo file)
}
catch (UnauthorizedAccessException)
{
this.Logger.LogWarning($"Unauthorized access exception caught when trying to open {file.FullName}");
this.logger.LogWarning("Unauthorized access exception caught when trying to open {FileName}", file.FullName);
return null;
}
catch (Exception e)
{
this.Logger.LogWarning($"Unhandled exception caught when trying to open {file.FullName}");
this.Logger.LogException(e, isError: false);
this.logger.LogWarning(e, "Unhandled exception caught when trying to open {FileName}", file.FullName);
return null;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
namespace Microsoft.ComponentDetection.Common;

using System;
using System.Collections.Generic;
using System.IO;
using Microsoft.ComponentDetection.Contracts;
using Microsoft.Extensions.Logging;

public class ComponentStreamEnumerableFactory : IComponentStreamEnumerableFactory
{
private readonly IPathUtilityService pathUtilityService;
private readonly ILogger logger;
private readonly ILogger<ComponentStreamEnumerable> logger;

public ComponentStreamEnumerableFactory(IPathUtilityService pathUtilityService, ILogger logger)
public ComponentStreamEnumerableFactory(IPathUtilityService pathUtilityService, ILogger<ComponentStreamEnumerable> logger)
{
this.pathUtilityService = pathUtilityService;
this.logger = logger;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,19 @@

namespace Microsoft.ComponentDetection.Common.DependencyGraph;

using Microsoft.Extensions.Logging;

public class ComponentRecorder : IComponentRecorder
{
private readonly ILogger log;

private readonly ConcurrentBag<SingleFileComponentRecorder> singleFileRecorders = new ConcurrentBag<SingleFileComponentRecorder>();

private readonly bool enableManualTrackingOfExplicitReferences;

public ComponentRecorder(ILogger log = null, bool enableManualTrackingOfExplicitReferences = true)
private readonly ILogger logger;

public ComponentRecorder(ILogger logger = null, bool enableManualTrackingOfExplicitReferences = true)
{
this.log = log;
this.logger = logger;
this.enableManualTrackingOfExplicitReferences = enableManualTrackingOfExplicitReferences;
}

Expand Down Expand Up @@ -86,7 +88,7 @@ public ISingleFileComponentRecorder CreateSingleFileComponentRecorder(string loc
var matching = this.singleFileRecorders.FirstOrDefault(x => x.ManifestFileLocation == location);
if (matching == null)
{
matching = new SingleFileComponentRecorder(location, this, this.enableManualTrackingOfExplicitReferences, this.log);
matching = new SingleFileComponentRecorder(location, this, this.enableManualTrackingOfExplicitReferences, this.logger);
this.singleFileRecorders.Add(matching);
}

Expand All @@ -106,8 +108,6 @@ internal DependencyGraph GetDependencyGraphForLocation(string location)

public sealed class SingleFileComponentRecorder : ISingleFileComponentRecorder
{
private readonly ILogger log;

private readonly ConcurrentDictionary<string, DetectedComponent> detectedComponentsInternal = new ConcurrentDictionary<string, DetectedComponent>();

/// <summary>
Expand All @@ -116,14 +116,15 @@ public sealed class SingleFileComponentRecorder : ISingleFileComponentRecorder
private readonly ConcurrentDictionary<string, byte> skippedComponentsInternal = new ConcurrentDictionary<string, byte>();

private readonly ComponentRecorder recorder;
private readonly ILogger logger;

private readonly object registerUsageLock = new object();

public SingleFileComponentRecorder(string location, ComponentRecorder recorder, bool enableManualTrackingOfExplicitReferences, ILogger log)
public SingleFileComponentRecorder(string location, ComponentRecorder recorder, bool enableManualTrackingOfExplicitReferences, ILogger logger)
{
this.ManifestFileLocation = location;
this.recorder = recorder;
this.log = log;
this.logger = logger;
this.DependencyGraph = new DependencyGraph(enableManualTrackingOfExplicitReferences);
}

Expand Down Expand Up @@ -174,17 +175,17 @@ public void RegisterUsage(
#if DEBUG
if (detectedComponent.FilePaths?.Any() ?? false)
{
this.log?.LogWarning("Detector should not populate DetectedComponent.FilePaths!");
this.logger.LogWarning("Detector should not populate DetectedComponent.FilePaths!");
}

if (detectedComponent.DependencyRoots?.Any() ?? false)
{
this.log?.LogWarning("Detector should not populate DetectedComponent.DependencyRoots!");
this.logger.LogWarning("Detector should not populate DetectedComponent.DependencyRoots!");
}

if (detectedComponent.DevelopmentDependency.HasValue)
{
this.log?.LogWarning("Detector should not populate DetectedComponent.DevelopmentDependency!");
this.logger.LogWarning("Detector should not populate DetectedComponent.DevelopmentDependency!");
}
#endif

Expand Down
6 changes: 4 additions & 2 deletions src/Microsoft.ComponentDetection.Common/DockerService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
namespace Microsoft.ComponentDetection.Common;

using System;
using System.Collections.Generic;
using System.IO;
Expand All @@ -10,6 +11,7 @@ namespace Microsoft.ComponentDetection.Common;
using Microsoft.ComponentDetection.Common.Telemetry.Records;
using Microsoft.ComponentDetection.Contracts;
using Microsoft.ComponentDetection.Contracts.BcdeModels;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;

public class DockerService : IDockerService
Expand All @@ -23,7 +25,7 @@ public class DockerService : IDockerService

private readonly ILogger logger;

public DockerService(ILogger logger) => this.logger = logger;
public DockerService(ILogger<DockerService> logger) => this.logger = logger;

public async Task<bool> CanPingDockerAsync(CancellationToken cancellationToken = default)
{
Expand All @@ -34,7 +36,7 @@ public async Task<bool> CanPingDockerAsync(CancellationToken cancellationToken =
}
catch (Exception e)
{
this.logger.LogException(e, false);
this.logger.LogError(e, "Failed to ping docker");
return false;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
namespace Microsoft.ComponentDetection.Common;

using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
Expand All @@ -12,14 +13,15 @@
using System.Threading.Tasks.Dataflow;
using Microsoft.ComponentDetection.Contracts;
using Microsoft.ComponentDetection.Contracts.Internal;
using Microsoft.Extensions.Logging;

public class FastDirectoryWalkerFactory : IObservableDirectoryWalkerFactory
{
private readonly ConcurrentDictionary<DirectoryInfo, Lazy<IObservable<FileSystemInfo>>> pendingScans = new ConcurrentDictionary<DirectoryInfo, Lazy<IObservable<FileSystemInfo>>>();
private readonly IPathUtilityService pathUtilityService;
private readonly ILogger logger;
private readonly ILogger<FastDirectoryWalkerFactory> logger;

public FastDirectoryWalkerFactory(IPathUtilityService pathUtilityService, ILogger logger)
public FastDirectoryWalkerFactory(IPathUtilityService pathUtilityService, ILogger<FastDirectoryWalkerFactory> logger)
{
this.pathUtilityService = pathUtilityService;
this.logger = logger;
Expand All @@ -31,7 +33,7 @@ public IObservable<FileSystemInfo> GetDirectoryScanner(DirectoryInfo root, Concu
{
if (!root.Exists)
{
this.logger?.LogError($"Root directory doesn't exist: {root.FullName}");
this.logger.LogError("Root directory doesn't exist: {RootFullName}", root.FullName);
s.OnCompleted();
return Task.CompletedTask;
}
Expand All @@ -49,7 +51,7 @@ public IObservable<FileSystemInfo> GetDirectoryScanner(DirectoryInfo root, Concu

var sw = Stopwatch.StartNew();

this.logger?.LogInfo($"Starting enumeration of {root.FullName}");
this.logger.LogInformation("Starting enumeration of {RootFullName}", root.FullName);

var fileCount = 0;
var directoryCount = 0;
Expand Down Expand Up @@ -187,7 +189,7 @@ public IObservable<FileSystemInfo> GetDirectoryScanner(DirectoryInfo root, Concu
() =>
{
sw.Stop();
this.logger?.LogInfo($"Enumerated {fileCount} files and {directoryCount} directories in {sw.Elapsed}");
this.logger.LogInformation("Enumerated {FileCount} files and {DirectoryCount} directories in {Elapsed}", fileCount, directoryCount, sw.Elapsed);
s.OnCompleted();
});
});
Expand All @@ -211,7 +213,7 @@ public IObservable<FileSystemInfo> Subscribe(DirectoryInfo root, IEnumerable<str

if (this.pendingScans.TryGetValue(root, out var scannerObservable))
{
this.logger.LogVerbose(string.Join(":", patterns));
this.logger.LogDebug("Logging patterns {Patterns} for {Root}", string.Join(":", patterns), root.FullName);

var inner = scannerObservable.Value.Where(fsi =>
{
Expand Down
11 changes: 6 additions & 5 deletions src/Microsoft.ComponentDetection.Common/LazyComponentStream.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
namespace Microsoft.ComponentDetection.Common;

using System;
using System.IO;
using Microsoft.ComponentDetection.Contracts;
using Microsoft.Extensions.Logging;

public class LazyComponentStream : IComponentStream
{
private readonly FileInfo fileInfo;
private readonly Lazy<byte[]> fileBuffer;
private readonly ILogger logger;
private readonly Lazy<byte[]> fileBuffer;

public LazyComponentStream(FileInfo fileInfo, string pattern, ILogger logger)
{
Expand Down Expand Up @@ -35,14 +37,13 @@ private byte[] SafeOpenFile()

return buffer;
}
catch (UnauthorizedAccessException)
catch (UnauthorizedAccessException e)
{
this.logger?.LogWarning($"Unauthorized access exception caught when trying to open {this.fileInfo.FullName}");
this.logger.LogWarning(e, "Unauthorized access exception caught when trying to open {FileName}", this.fileInfo.FullName);
}
catch (Exception e)
{
this.logger?.LogWarning($"Unhandled exception caught when trying to open {this.fileInfo.FullName}");
this.logger?.LogException(e, isError: false);
this.logger.LogWarning(e, "Unhandled exception caught when trying to open {FileName}", this.fileInfo.FullName);
}

return Array.Empty<byte>();
Expand Down
Loading

0 comments on commit 273d48e

Please sign in to comment.