Skip to content

Commit 3105697

Browse files
authored
Merge pull request github#16200 from michaelnebel/csharp/dependencylogging
C#: Logging
2 parents feeaef1 + 8915241 commit 3105697

File tree

10 files changed

+49
-48
lines changed

10 files changed

+49
-48
lines changed

csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/AssemblyLookupLocation.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ private void AddReferenceDirectory(List<string> dllsToIndex, ILogger logger)
4343
}
4444
else
4545
{
46-
logger.LogInfo($"AssemblyLookupLocation: Skipping {dll.FullName}.");
46+
logger.LogDebug($"AssemblyLookupLocation: Skipping {dll.FullName}.");
4747
}
4848
}
4949
}
@@ -68,19 +68,19 @@ public List<string> GetDlls(ILogger logger)
6868
}
6969
else
7070
{
71-
logger.LogInfo($"AssemblyLookupLocation: Skipping {path}.");
71+
logger.LogDebug($"AssemblyLookupLocation: Skipping {path}.");
7272
}
7373
return dllsToIndex;
7474
}
7575

7676
if (Directory.Exists(path))
7777
{
78-
logger.LogInfo($"AssemblyLookupLocation: Finding reference DLLs in {path}...");
78+
logger.LogDebug($"AssemblyLookupLocation: Finding reference DLLs in {path}...");
7979
AddReferenceDirectory(dllsToIndex, logger);
8080
}
8181
else
8282
{
83-
logger.LogInfo("AssemblyLookupLocation: Path not found: " + path);
83+
logger.LogDebug("AssemblyLookupLocation: Path not found: " + path);
8484
}
8585
return dllsToIndex;
8686
}

csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/DependencyManager.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -141,12 +141,12 @@ void exitCallback(int ret, string msg, bool silent)
141141
// Output the findings
142142
foreach (var r in usedReferences.Keys.OrderBy(r => r))
143143
{
144-
logger.LogInfo($"Resolved reference {r}");
144+
logger.LogDebug($"Resolved reference {r}");
145145
}
146146

147147
foreach (var r in unresolvedReferences.OrderBy(r => r.Key))
148148
{
149-
logger.LogInfo($"Unresolved reference {r.Key} in project {r.Value}");
149+
logger.LogDebug($"Unresolved reference {r.Key} in project {r.Value}");
150150
}
151151

152152
var webViewExtractionOption = Environment.GetEnvironmentVariable(EnvironmentVariableNames.WebViewGeneration);
@@ -253,7 +253,7 @@ private void RemoveNugetAnalyzerReferences()
253253
if (isInAnalyzersFolder)
254254
{
255255
usedReferences.Remove(filename);
256-
logger.LogInfo($"Removed analyzer reference {filename}");
256+
logger.LogDebug($"Removed analyzer reference {filename}");
257257
}
258258
}
259259
}
@@ -265,19 +265,19 @@ private void SelectNewestFrameworkPath(string frameworkPath, string frameworkTyp
265265
if (versionFolders.Length > 1)
266266
{
267267
var versions = string.Join(", ", versionFolders.Select(d => d.Name));
268-
logger.LogInfo($"Found multiple {frameworkType} DLLs in NuGet packages at {frameworkPath}. Using the latest version ({versionFolders[0].Name}) from: {versions}.");
268+
logger.LogDebug($"Found multiple {frameworkType} DLLs in NuGet packages at {frameworkPath}. Using the latest version ({versionFolders[0].Name}) from: {versions}.");
269269
}
270270

271271
var selectedFrameworkFolder = versionFolders.FirstOrDefault()?.FullName;
272272
if (selectedFrameworkFolder is null)
273273
{
274-
logger.LogInfo($"Found {frameworkType} DLLs in NuGet packages at {frameworkPath}, but no version folder was found.");
274+
logger.LogDebug($"Found {frameworkType} DLLs in NuGet packages at {frameworkPath}, but no version folder was found.");
275275
selectedFrameworkFolder = frameworkPath;
276276
}
277277

278278
dllLocations.Add(selectedFrameworkFolder);
279279
frameworkLocations.Add(selectedFrameworkFolder);
280-
logger.LogInfo($"Found {frameworkType} DLLs in NuGet packages at {selectedFrameworkFolder}.");
280+
logger.LogDebug($"Found {frameworkType} DLLs in NuGet packages at {selectedFrameworkFolder}.");
281281
}
282282

283283
private static DirectoryInfo[] GetPackageVersionSubDirectories(string packagePath)
@@ -361,7 +361,7 @@ private void RemoveNugetPackageReference(string packagePrefix, ISet<AssemblyLook
361361
foreach (var path in toRemove)
362362
{
363363
dllLocations.Remove(path);
364-
logger.LogInfo($"Removed reference {path}");
364+
logger.LogDebug($"Removed reference {path}");
365365
}
366366
}
367367

@@ -561,7 +561,7 @@ private void ResolveConflicts(IEnumerable<string> frameworkPaths)
561561
if (resolvedInfo.Version != r.Version || resolvedInfo.NetCoreVersion != r.NetCoreVersion)
562562
{
563563
var asm = resolvedInfo.Id + (resolvedInfo.NetCoreVersion is null ? "" : $" (.NET Core {resolvedInfo.NetCoreVersion})");
564-
logger.LogInfo($"Resolved {r.Id} as {asm}");
564+
logger.LogDebug($"Resolved {r.Id} as {asm}");
565565

566566
++conflictedReferences;
567567
}

csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/DotNet.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ private DotNet(ILogger logger, string? dotNetPath, TemporaryDirectory tempWorkin
3535

3636
private void Info()
3737
{
38-
var res = dotnetCliInvoker.RunCommand("--info");
38+
var res = dotnetCliInvoker.RunCommand("--info", silent: false);
3939
if (!res)
4040
{
4141
throw new Exception($"{dotnetCliInvoker.Exec} --info failed.");
@@ -91,13 +91,13 @@ public bool AddPackage(string folder, string package)
9191
return dotnetCliInvoker.RunCommand(args);
9292
}
9393

94-
public IList<string> GetListedRuntimes() => GetResultList("--list-runtimes");
94+
public IList<string> GetListedRuntimes() => GetResultList("--list-runtimes", null, false);
9595

96-
public IList<string> GetListedSdks() => GetResultList("--list-sdks");
96+
public IList<string> GetListedSdks() => GetResultList("--list-sdks", null, false);
9797

98-
private IList<string> GetResultList(string args, string? workingDirectory = null)
98+
private IList<string> GetResultList(string args, string? workingDirectory = null, bool silent = true)
9999
{
100-
if (dotnetCliInvoker.RunCommand(args, workingDirectory, out var results))
100+
if (dotnetCliInvoker.RunCommand(args, workingDirectory, out var results, silent))
101101
{
102102
return results;
103103
}
@@ -316,4 +316,4 @@ public static BuildScript WithDotNet(IBuildActions actions, ILogger logger, IEnu
316316
});
317317
}
318318
}
319-
}
319+
}

csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/DotNetCliInvoker.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,13 @@ private ProcessStartInfo MakeDotnetStartInfo(string args, string? workingDirecto
4040
return startInfo;
4141
}
4242

43-
private bool RunCommandAux(string args, string? workingDirectory, out IList<string> output)
43+
private bool RunCommandAux(string args, string? workingDirectory, out IList<string> output, bool silent)
4444
{
4545
var dirLog = string.IsNullOrWhiteSpace(workingDirectory) ? "" : $" in {workingDirectory}";
4646
logger.LogInfo($"Running {Exec} {args}{dirLog}");
4747
var pi = MakeDotnetStartInfo(args, workingDirectory);
4848
var threadId = Environment.CurrentManagedThreadId;
49-
void onOut(string s) => logger.LogInfo(s, threadId);
49+
void onOut(string s) => logger.Log(silent ? Severity.Debug : Severity.Info, s, threadId);
5050
void onError(string s) => logger.LogError(s, threadId);
5151
var exitCode = pi.ReadOutput(out output, onOut, onError);
5252
if (exitCode != 0)
@@ -57,13 +57,13 @@ private bool RunCommandAux(string args, string? workingDirectory, out IList<stri
5757
return true;
5858
}
5959

60-
public bool RunCommand(string args) =>
61-
RunCommandAux(args, null, out _);
60+
public bool RunCommand(string args, bool silent) =>
61+
RunCommandAux(args, null, out _, silent);
6262

63-
public bool RunCommand(string args, out IList<string> output) =>
64-
RunCommandAux(args, null, out output);
63+
public bool RunCommand(string args, out IList<string> output, bool silent) =>
64+
RunCommandAux(args, null, out output, silent);
6565

66-
public bool RunCommand(string args, string? workingDirectory, out IList<string> output) =>
67-
RunCommandAux(args, workingDirectory, out output);
66+
public bool RunCommand(string args, string? workingDirectory, out IList<string> output, bool silent) =>
67+
RunCommandAux(args, workingDirectory, out output, silent);
6868
}
6969
}

csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/IDotNetCliInvoker.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,22 @@ internal interface IDotNetCliInvoker
1111

1212
/// <summary>
1313
/// Execute `dotnet <args>` and return true if the command succeeded, otherwise false.
14+
/// If `silent` is true the output of the command is logged as `debug` otherwise as `info`.
1415
/// </summary>
15-
bool RunCommand(string args);
16+
bool RunCommand(string args, bool silent = true);
1617

1718
/// <summary>
1819
/// Execute `dotnet <args>` and return true if the command succeeded, otherwise false.
1920
/// The output of the command is returned in `output`.
21+
/// If `silent` is true the output of the command is logged as `debug` otherwise as `info`.
2022
/// </summary>
21-
bool RunCommand(string args, out IList<string> output);
23+
bool RunCommand(string args, out IList<string> output, bool silent = true);
2224

2325
/// <summary>
2426
/// Execute `dotnet <args>` in `<workingDirectory>` and return true if the command succeeded, otherwise false.
2527
/// The output of the command is returned in `output`.
28+
/// If `silent` is true the output of the command is logged as `debug` otherwise as `info`.
2629
/// </summary>
27-
bool RunCommand(string args, string? workingDirectory, out IList<string> output);
30+
bool RunCommand(string args, string? workingDirectory, out IList<string> output, bool silent = true);
2831
}
2932
}

csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/NugetExeWrapper.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ private bool TryRestoreNugetPackage(string package)
175175
};
176176

177177
var threadId = Environment.CurrentManagedThreadId;
178-
void onOut(string s) => logger.LogInfo(s, threadId);
178+
void onOut(string s) => logger.LogDebug(s, threadId);
179179
void onError(string s) => logger.LogError(s, threadId);
180180
var exitCode = pi.ReadOutput(out var _, onOut, onError);
181181
if (exitCode != 0)
@@ -235,7 +235,7 @@ private void RunMonoNugetCommand(string command, out IList<string> stdout)
235235
};
236236

237237
var threadId = Environment.CurrentManagedThreadId;
238-
void onOut(string s) => logger.LogInfo(s, threadId);
238+
void onOut(string s) => logger.LogDebug(s, threadId);
239239
void onError(string s) => logger.LogError(s, threadId);
240240
pi.ReadOutput(out stdout, onOut, onError);
241241
}

csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/NugetPackageRestorer.cs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ private void LogAllUnusedPackages(DependencyContainer dependencies)
361361
allPackageDirectories
362362
.Where(package => !dependencies.Packages.Contains(package))
363363
.Order()
364-
.ForEach(package => logger.LogInfo($"Unused package: {package}"));
364+
.ForEach(package => logger.LogDebug($"Unused package: {package}"));
365365
}
366366

367367
private ICollection<string> GetAllPackageDirectories()
@@ -623,13 +623,9 @@ private IEnumerable<string> GetFeeds(Func<IList<string>> getNugetFeeds)
623623

624624
private (HashSet<string> explicitFeeds, HashSet<string> allFeeds) GetAllFeeds()
625625
{
626-
IList<string> GetNugetFeeds(string nugetConfig) => dotnet.GetNugetFeeds(nugetConfig);
627-
628-
IList<string> GetNugetFeedsFromFolder(string folderPath) => dotnet.GetNugetFeedsFromFolder(folderPath);
629-
630626
var nugetConfigs = fileProvider.NugetConfigs;
631627
var explicitFeeds = nugetConfigs
632-
.SelectMany(config => GetFeeds(() => GetNugetFeeds(config)))
628+
.SelectMany(config => GetFeeds(() => dotnet.GetNugetFeeds(config)))
633629
.ToHashSet();
634630

635631
if (explicitFeeds.Count > 0)
@@ -657,7 +653,7 @@ private IEnumerable<string> GetFeeds(Func<IList<string>> getNugetFeeds)
657653
return null;
658654
})
659655
.Where(folder => folder != null)
660-
.SelectMany(folder => GetFeeds(() => GetNugetFeedsFromFolder(folder!)))
656+
.SelectMany(folder => GetFeeds(() => dotnet.GetNugetFeedsFromFolder(folder!)))
661657
.ToHashSet();
662658

663659
logger.LogInfo($"Found {allFeeds.Count} Nuget feeds (with inherited ones) in nuget.config files: {string.Join(", ", allFeeds.OrderBy(f => f))}");

csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/Razor.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,10 @@ public IEnumerable<string> GenerateFiles(IEnumerable<string> cshtmls, IEnumerabl
6464

6565
try
6666
{
67+
logger.LogInfo("Produce analyzer config content.");
6768
GenerateAnalyzerConfig(cshtmls, analyzerConfig);
6869

69-
logger.LogInfo($"Analyzer config content: {File.ReadAllText(analyzerConfig)}");
70+
logger.LogDebug($"Analyzer config content: {File.ReadAllText(analyzerConfig)}");
7071

7172
var args = new StringBuilder();
7273
args.Append($"/target:exe /generatedfilesout:\"{outputFolder}\" /out:\"{dllPath}\" /analyzerconfig:\"{analyzerConfig}\" ");
@@ -88,7 +89,8 @@ public IEnumerable<string> GenerateFiles(IEnumerable<string> cshtmls, IEnumerabl
8889

8990
var argsString = args.ToString();
9091

91-
logger.LogInfo($"Running CSC to generate Razor source files with arguments: {argsString}.");
92+
logger.LogInfo($"Running CSC to generate Razor source files.");
93+
logger.LogDebug($"Running CSC to generate Razor source files with arguments: {argsString}.");
9294

9395
using (var sw = new StreamWriter(cscArgsPath))
9496
{
@@ -126,4 +128,4 @@ private void DeleteFile(string path)
126128
}
127129
}
128130
}
129-
}
131+
}

csharp/extractor/Semmle.Extraction.CSharp.Standalone/Extractor.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,12 +111,12 @@ public void Analysed(int item, int total, string source, string output, TimeSpan
111111
AnalysisAction.UpToDate => "up to date",
112112
_ => "unknown action"
113113
};
114-
logger.LogInfo($"[{item}/{total}] {source} ({extra})");
114+
logger.LogDebug($"[{item}/{total}] {source} ({extra})");
115115
}
116116

117117
public void Started(int item, int total, string source)
118118
{
119-
logger.LogInfo($"[{item}/{total}] {source} (processing started)");
119+
logger.LogDebug($"[{item}/{total}] {source} (processing started)");
120120
}
121121

122122
public void MissingType(string type)
@@ -166,4 +166,4 @@ public static ExitCode Run(Options options)
166166
return ExitCode.Ok;
167167
}
168168
}
169-
}
169+
}

csharp/extractor/Semmle.Extraction.Tests/DotNet.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,23 +20,23 @@ public DotNetCliInvokerStub(IList<string> output)
2020

2121
public string Exec => "dotnet";
2222

23-
public bool RunCommand(string args)
23+
public bool RunCommand(string args, bool silent)
2424
{
2525
lastArgs = args;
2626
return Success;
2727
}
2828

29-
public bool RunCommand(string args, out IList<string> output)
29+
public bool RunCommand(string args, out IList<string> output, bool silent)
3030
{
3131
lastArgs = args;
3232
output = this.output;
3333
return Success;
3434
}
3535

36-
public bool RunCommand(string args, string? workingDirectory, out IList<string> output)
36+
public bool RunCommand(string args, string? workingDirectory, out IList<string> output, bool silent)
3737
{
3838
WorkingDirectory = workingDirectory ?? "";
39-
return RunCommand(args, out output);
39+
return RunCommand(args, out output, silent);
4040
}
4141

4242
public string GetLastArgs() => lastArgs;

0 commit comments

Comments
 (0)