Skip to content

Commit 273d48e

Browse files
authored
feat: Switch to Serilog (#441)
1 parent ca02614 commit 273d48e

File tree

90 files changed

+651
-904
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

90 files changed

+651
-904
lines changed

.editorconfig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -669,6 +669,9 @@ dotnet_diagnostic.CA1852.severity = suggestion
669669
# CA1854: Prefer the IDictionary.TryGetValue(TKey, out TValue) method
670670
dotnet_diagnostic.CA1854.severity = suggestion
671671

672+
# CA1848: Use the LoggerMessage delegates
673+
dotnet_diagnostic.CA1848.severity = suggestion
674+
672675
# JSON002: Probable JSON string detected
673676
dotnet_diagnostic.JSON002.severity = suggestion
674677

Directory.Packages.props

Lines changed: 46 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,48 @@
11
<Project>
2-
3-
<ItemDefinitionGroup>
4-
<PackageReference>
5-
<!-- Do not share compile-time dependencies transitively. This requires that all projects reference all packages -->
6-
<PrivateAssets>Compile</PrivateAssets>
7-
</PackageReference>
8-
</ItemDefinitionGroup>
9-
10-
<ItemGroup>
11-
<PackageVersion Include="CommandLineParser" Version="2.9.1"/>
12-
<PackageVersion Include="coverlet.msbuild" Version="3.2.0"/>
13-
<PackageVersion Include="Docker.DotNet" Version="3.125.12"/>
14-
<PackageVersion Include="FluentAssertions" Version="6.10.0"/>
15-
<PackageVersion Include="Microsoft.AspNet.WebApi.Client" Version="5.2.9"/>
16-
<PackageVersion Include="Microsoft.Extensions.Caching.Memory" Version="7.0.0"/>
17-
<PackageVersion Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0"/>
18-
<PackageVersion Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="7.0.0"/>
19-
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.5.0"/>
20-
<PackageVersion Include="Microsoft.SourceLink.GitHub" Version="1.1.1"/>
21-
<PackageVersion Include="Microsoft.VisualStudio.Threading.Analyzers" Version="17.5.22"/>
22-
<PackageVersion Include="DotNet.Glob" Version="2.1.1"/>
23-
<PackageVersion Include="MinVer" Version="4.3.0"/>
24-
<PackageVersion Include="Moq" Version="4.18.4"/>
25-
<PackageVersion Include="morelinq" Version="3.3.2"/>
26-
<PackageVersion Include="MSTest.TestAdapter" Version="3.0.2"/>
27-
<PackageVersion Include="MSTest.TestFramework" Version="3.0.2"/>
28-
<PackageVersion Include="Newtonsoft.Json" Version="13.0.2"/>
29-
<PackageVersion Include="NuGet.ProjectModel" Version="6.5.0"/>
30-
<PackageVersion Include="NuGet.Versioning" Version="6.5.0"/>
31-
<PackageVersion Include="packageurl-dotnet" Version="1.0.0"/>
32-
<PackageVersion Include="Polly" Version="7.2.3"/>
33-
<PackageVersion Include="Semver" Version="2.3.0"/>
34-
<PackageVersion Include="StyleCop.Analyzers" Version="1.2.0-beta.435"/>
35-
<PackageVersion Include="System.Memory" Version="4.5.5"/>
36-
<PackageVersion Include="System.Reactive" Version="5.0.0"/>
37-
<PackageVersion Include="System.Runtime.Loader" Version="4.3.0"/>
38-
<PackageVersion Include="System.Threading.Tasks.Dataflow" Version="7.0.0"/>
39-
<PackageVersion Include="Tomlyn" Version="0.16.2"/>
40-
<PackageVersion Include="yamldotnet" Version="13.0.1"/>
41-
<PackageVersion Include="Faker.net" Version="2.0.154"/>
42-
<PackageVersion Include="Valleysoft.DockerfileModel" Version="1.1.0"/>
43-
</ItemGroup>
2+
<ItemDefinitionGroup>
3+
<PackageReference>
4+
<!-- Do not share compile-time dependencies transitively. This requires that all projects reference all packages -->
5+
<PrivateAssets>Compile</PrivateAssets>
6+
</PackageReference>
7+
</ItemDefinitionGroup>
8+
<ItemGroup>
9+
<PackageVersion Include="CommandLineParser" Version="2.9.1" />
10+
<PackageVersion Include="coverlet.msbuild" Version="3.2.0" />
11+
<PackageVersion Include="Docker.DotNet" Version="3.125.12" />
12+
<PackageVersion Include="FluentAssertions" Version="6.10.0" />
13+
<PackageVersion Include="Microsoft.AspNet.WebApi.Client" Version="5.2.9" />
14+
<PackageVersion Include="Microsoft.Extensions.Caching.Memory" Version="7.0.0" />
15+
<PackageVersion Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
16+
<PackageVersion Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="7.0.0" />
17+
<PackageVersion Include="Microsoft.Extensions.Logging" Version="7.0.0" />
18+
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
19+
<PackageVersion Include="Microsoft.SourceLink.GitHub" Version="1.1.1" />
20+
<PackageVersion Include="Microsoft.VisualStudio.Threading.Analyzers" Version="17.5.22" />
21+
<PackageVersion Include="DotNet.Glob" Version="2.1.1" />
22+
<PackageVersion Include="MinVer" Version="4.3.0" />
23+
<PackageVersion Include="Moq" Version="4.18.4" />
24+
<PackageVersion Include="morelinq" Version="3.3.2" />
25+
<PackageVersion Include="MSTest.TestAdapter" Version="3.0.2" />
26+
<PackageVersion Include="MSTest.TestFramework" Version="3.0.2" />
27+
<PackageVersion Include="Newtonsoft.Json" Version="13.0.2" />
28+
<PackageVersion Include="NuGet.ProjectModel" Version="6.5.0" />
29+
<PackageVersion Include="NuGet.Versioning" Version="6.5.0" />
30+
<PackageVersion Include="packageurl-dotnet" Version="1.0.0" />
31+
<PackageVersion Include="Polly" Version="7.2.3" />
32+
<PackageVersion Include="Semver" Version="2.2.0" />
33+
<PackageVersion Include="Serilog" Version="2.12.0" />
34+
<PackageVersion Include="Serilog.Extensions.Hosting" Version="5.0.1" />
35+
<PackageVersion Include="Serilog.Extensions.Logging" Version="3.1.0" />
36+
<PackageVersion Include="Serilog.Sinks.Console" Version="4.1.0" />
37+
<PackageVersion Include="Serilog.Sinks.File" Version="5.0.0" />
38+
<PackageVersion Include="StyleCop.Analyzers" Version="1.2.0-beta.435" />
39+
<PackageVersion Include="System.Memory" Version="4.5.5" />
40+
<PackageVersion Include="System.Reactive" Version="5.0.0" />
41+
<PackageVersion Include="System.Runtime.Loader" Version="4.3.0" />
42+
<PackageVersion Include="System.Threading.Tasks.Dataflow" Version="7.0.0" />
43+
<PackageVersion Include="Tomlyn" Version="0.16.2" />
44+
<PackageVersion Include="yamldotnet" Version="13.0.1" />
45+
<PackageVersion Include="Faker.net" Version="2.0.154" />
46+
<PackageVersion Include="Valleysoft.DockerfileModel" Version="1.1.0" />
47+
</ItemGroup>
4448
</Project>

src/Microsoft.ComponentDetection.Common/ComponentStreamEnumerable.cs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,31 @@
11
namespace Microsoft.ComponentDetection.Common;
2+
23
using System;
34
using System.Collections;
45
using System.Collections.Generic;
56
using System.IO;
67
using Microsoft.ComponentDetection.Contracts;
8+
using Microsoft.Extensions.Logging;
79

810
public class ComponentStreamEnumerable : IEnumerable<IComponentStream>
911
{
12+
private readonly ILogger logger;
13+
1014
public ComponentStreamEnumerable(IEnumerable<MatchedFile> fileEnumerable, ILogger logger)
1115
{
16+
this.logger = logger;
1217
this.ToEnumerate = fileEnumerable;
13-
this.Logger = logger;
1418
}
1519

1620
private IEnumerable<MatchedFile> ToEnumerate { get; }
1721

18-
private ILogger Logger { get; }
19-
2022
public IEnumerator<IComponentStream> GetEnumerator()
2123
{
2224
foreach (var filePairing in this.ToEnumerate)
2325
{
2426
if (!filePairing.File.Exists)
2527
{
26-
this.Logger.LogWarning($"File {filePairing.File.FullName} does not exist on disk.");
28+
this.logger.LogWarning("File {FilePairingName} does not exist on disk.", filePairing.File.FullName);
2729
yield break;
2830
}
2931

@@ -51,13 +53,12 @@ private Stream SafeOpenFile(FileInfo file)
5153
}
5254
catch (UnauthorizedAccessException)
5355
{
54-
this.Logger.LogWarning($"Unauthorized access exception caught when trying to open {file.FullName}");
56+
this.logger.LogWarning("Unauthorized access exception caught when trying to open {FileName}", file.FullName);
5557
return null;
5658
}
5759
catch (Exception e)
5860
{
59-
this.Logger.LogWarning($"Unhandled exception caught when trying to open {file.FullName}");
60-
this.Logger.LogException(e, isError: false);
61+
this.logger.LogWarning(e, "Unhandled exception caught when trying to open {FileName}", file.FullName);
6162
return null;
6263
}
6364
}

src/Microsoft.ComponentDetection.Common/ComponentStreamEnumerableFactory.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
namespace Microsoft.ComponentDetection.Common;
2+
23
using System;
34
using System.Collections.Generic;
45
using System.IO;
56
using Microsoft.ComponentDetection.Contracts;
7+
using Microsoft.Extensions.Logging;
68

79
public class ComponentStreamEnumerableFactory : IComponentStreamEnumerableFactory
810
{
911
private readonly IPathUtilityService pathUtilityService;
10-
private readonly ILogger logger;
12+
private readonly ILogger<ComponentStreamEnumerable> logger;
1113

12-
public ComponentStreamEnumerableFactory(IPathUtilityService pathUtilityService, ILogger logger)
14+
public ComponentStreamEnumerableFactory(IPathUtilityService pathUtilityService, ILogger<ComponentStreamEnumerable> logger)
1315
{
1416
this.pathUtilityService = pathUtilityService;
1517
this.logger = logger;

src/Microsoft.ComponentDetection.Common/DependencyGraph/ComponentRecorder.cs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,19 @@
1212

1313
namespace Microsoft.ComponentDetection.Common.DependencyGraph;
1414

15+
using Microsoft.Extensions.Logging;
16+
1517
public class ComponentRecorder : IComponentRecorder
1618
{
17-
private readonly ILogger log;
18-
1919
private readonly ConcurrentBag<SingleFileComponentRecorder> singleFileRecorders = new ConcurrentBag<SingleFileComponentRecorder>();
2020

2121
private readonly bool enableManualTrackingOfExplicitReferences;
2222

23-
public ComponentRecorder(ILogger log = null, bool enableManualTrackingOfExplicitReferences = true)
23+
private readonly ILogger logger;
24+
25+
public ComponentRecorder(ILogger logger = null, bool enableManualTrackingOfExplicitReferences = true)
2426
{
25-
this.log = log;
27+
this.logger = logger;
2628
this.enableManualTrackingOfExplicitReferences = enableManualTrackingOfExplicitReferences;
2729
}
2830

@@ -86,7 +88,7 @@ public ISingleFileComponentRecorder CreateSingleFileComponentRecorder(string loc
8688
var matching = this.singleFileRecorders.FirstOrDefault(x => x.ManifestFileLocation == location);
8789
if (matching == null)
8890
{
89-
matching = new SingleFileComponentRecorder(location, this, this.enableManualTrackingOfExplicitReferences, this.log);
91+
matching = new SingleFileComponentRecorder(location, this, this.enableManualTrackingOfExplicitReferences, this.logger);
9092
this.singleFileRecorders.Add(matching);
9193
}
9294

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

107109
public sealed class SingleFileComponentRecorder : ISingleFileComponentRecorder
108110
{
109-
private readonly ILogger log;
110-
111111
private readonly ConcurrentDictionary<string, DetectedComponent> detectedComponentsInternal = new ConcurrentDictionary<string, DetectedComponent>();
112112

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

118118
private readonly ComponentRecorder recorder;
119+
private readonly ILogger logger;
119120

120121
private readonly object registerUsageLock = new object();
121122

122-
public SingleFileComponentRecorder(string location, ComponentRecorder recorder, bool enableManualTrackingOfExplicitReferences, ILogger log)
123+
public SingleFileComponentRecorder(string location, ComponentRecorder recorder, bool enableManualTrackingOfExplicitReferences, ILogger logger)
123124
{
124125
this.ManifestFileLocation = location;
125126
this.recorder = recorder;
126-
this.log = log;
127+
this.logger = logger;
127128
this.DependencyGraph = new DependencyGraph(enableManualTrackingOfExplicitReferences);
128129
}
129130

@@ -174,17 +175,17 @@ public void RegisterUsage(
174175
#if DEBUG
175176
if (detectedComponent.FilePaths?.Any() ?? false)
176177
{
177-
this.log?.LogWarning("Detector should not populate DetectedComponent.FilePaths!");
178+
this.logger.LogWarning("Detector should not populate DetectedComponent.FilePaths!");
178179
}
179180

180181
if (detectedComponent.DependencyRoots?.Any() ?? false)
181182
{
182-
this.log?.LogWarning("Detector should not populate DetectedComponent.DependencyRoots!");
183+
this.logger.LogWarning("Detector should not populate DetectedComponent.DependencyRoots!");
183184
}
184185

185186
if (detectedComponent.DevelopmentDependency.HasValue)
186187
{
187-
this.log?.LogWarning("Detector should not populate DetectedComponent.DevelopmentDependency!");
188+
this.logger.LogWarning("Detector should not populate DetectedComponent.DevelopmentDependency!");
188189
}
189190
#endif
190191

src/Microsoft.ComponentDetection.Common/DockerService.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
namespace Microsoft.ComponentDetection.Common;
2+
23
using System;
34
using System.Collections.Generic;
45
using System.IO;
@@ -10,6 +11,7 @@ namespace Microsoft.ComponentDetection.Common;
1011
using Microsoft.ComponentDetection.Common.Telemetry.Records;
1112
using Microsoft.ComponentDetection.Contracts;
1213
using Microsoft.ComponentDetection.Contracts.BcdeModels;
14+
using Microsoft.Extensions.Logging;
1315
using Newtonsoft.Json;
1416

1517
public class DockerService : IDockerService
@@ -23,7 +25,7 @@ public class DockerService : IDockerService
2325

2426
private readonly ILogger logger;
2527

26-
public DockerService(ILogger logger) => this.logger = logger;
28+
public DockerService(ILogger<DockerService> logger) => this.logger = logger;
2729

2830
public async Task<bool> CanPingDockerAsync(CancellationToken cancellationToken = default)
2931
{
@@ -34,7 +36,7 @@ public async Task<bool> CanPingDockerAsync(CancellationToken cancellationToken =
3436
}
3537
catch (Exception e)
3638
{
37-
this.logger.LogException(e, false);
39+
this.logger.LogError(e, "Failed to ping docker");
3840
return false;
3941
}
4042
}

src/Microsoft.ComponentDetection.Common/FastDirectoryWalkerFactory.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
namespace Microsoft.ComponentDetection.Common;
2+
23
using System;
34
using System.Collections.Concurrent;
45
using System.Collections.Generic;
@@ -12,14 +13,15 @@
1213
using System.Threading.Tasks.Dataflow;
1314
using Microsoft.ComponentDetection.Contracts;
1415
using Microsoft.ComponentDetection.Contracts.Internal;
16+
using Microsoft.Extensions.Logging;
1517

1618
public class FastDirectoryWalkerFactory : IObservableDirectoryWalkerFactory
1719
{
1820
private readonly ConcurrentDictionary<DirectoryInfo, Lazy<IObservable<FileSystemInfo>>> pendingScans = new ConcurrentDictionary<DirectoryInfo, Lazy<IObservable<FileSystemInfo>>>();
1921
private readonly IPathUtilityService pathUtilityService;
20-
private readonly ILogger logger;
22+
private readonly ILogger<FastDirectoryWalkerFactory> logger;
2123

22-
public FastDirectoryWalkerFactory(IPathUtilityService pathUtilityService, ILogger logger)
24+
public FastDirectoryWalkerFactory(IPathUtilityService pathUtilityService, ILogger<FastDirectoryWalkerFactory> logger)
2325
{
2426
this.pathUtilityService = pathUtilityService;
2527
this.logger = logger;
@@ -31,7 +33,7 @@ public IObservable<FileSystemInfo> GetDirectoryScanner(DirectoryInfo root, Concu
3133
{
3234
if (!root.Exists)
3335
{
34-
this.logger?.LogError($"Root directory doesn't exist: {root.FullName}");
36+
this.logger.LogError("Root directory doesn't exist: {RootFullName}", root.FullName);
3537
s.OnCompleted();
3638
return Task.CompletedTask;
3739
}
@@ -49,7 +51,7 @@ public IObservable<FileSystemInfo> GetDirectoryScanner(DirectoryInfo root, Concu
4951

5052
var sw = Stopwatch.StartNew();
5153

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

5456
var fileCount = 0;
5557
var directoryCount = 0;
@@ -187,7 +189,7 @@ public IObservable<FileSystemInfo> GetDirectoryScanner(DirectoryInfo root, Concu
187189
() =>
188190
{
189191
sw.Stop();
190-
this.logger?.LogInfo($"Enumerated {fileCount} files and {directoryCount} directories in {sw.Elapsed}");
192+
this.logger.LogInformation("Enumerated {FileCount} files and {DirectoryCount} directories in {Elapsed}", fileCount, directoryCount, sw.Elapsed);
191193
s.OnCompleted();
192194
});
193195
});
@@ -211,7 +213,7 @@ public IObservable<FileSystemInfo> Subscribe(DirectoryInfo root, IEnumerable<str
211213

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

216218
var inner = scannerObservable.Value.Where(fsi =>
217219
{

src/Microsoft.ComponentDetection.Common/LazyComponentStream.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
namespace Microsoft.ComponentDetection.Common;
2+
23
using System;
34
using System.IO;
45
using Microsoft.ComponentDetection.Contracts;
6+
using Microsoft.Extensions.Logging;
57

68
public class LazyComponentStream : IComponentStream
79
{
810
private readonly FileInfo fileInfo;
9-
private readonly Lazy<byte[]> fileBuffer;
1011
private readonly ILogger logger;
12+
private readonly Lazy<byte[]> fileBuffer;
1113

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

3638
return buffer;
3739
}
38-
catch (UnauthorizedAccessException)
40+
catch (UnauthorizedAccessException e)
3941
{
40-
this.logger?.LogWarning($"Unauthorized access exception caught when trying to open {this.fileInfo.FullName}");
42+
this.logger.LogWarning(e, "Unauthorized access exception caught when trying to open {FileName}", this.fileInfo.FullName);
4143
}
4244
catch (Exception e)
4345
{
44-
this.logger?.LogWarning($"Unhandled exception caught when trying to open {this.fileInfo.FullName}");
45-
this.logger?.LogException(e, isError: false);
46+
this.logger.LogWarning(e, "Unhandled exception caught when trying to open {FileName}", this.fileInfo.FullName);
4647
}
4748

4849
return Array.Empty<byte>();

0 commit comments

Comments
 (0)