Skip to content

Commit 9c139b5

Browse files
authored
Merge pull request github#16321 from tamasvajk/buildless/log-messages
C#: Improve log messages
2 parents 9082972 + dd9183c commit 9c139b5

File tree

7 files changed

+42
-34
lines changed

7 files changed

+42
-34
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ void exitCallback(int ret, string msg, bool silent)
191191

192192
private HashSet<string> AddFrameworkDlls(HashSet<AssemblyLookupLocation> dllLocations)
193193
{
194+
logger.LogInfo("Adding .NET Framework DLLs");
194195
var frameworkLocations = new HashSet<string>();
195196

196197
var frameworkReferences = Environment.GetEnvironmentVariable(EnvironmentVariableNames.DotnetFrameworkReferences);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,9 @@ public bool AddPackage(string folder, string package)
9191
return dotnetCliInvoker.RunCommand(args);
9292
}
9393

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

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

9898
private IList<string> GetResultList(string args, string? workingDirectory = null, bool silent = true)
9999
{

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public DotNetCliInvoker(ILogger logger, string exec)
1919
{
2020
this.logger = logger;
2121
this.Exec = exec;
22+
logger.LogInfo($"Using .NET CLI executable: '{Exec}'");
2223
}
2324

2425
private ProcessStartInfo MakeDotnetStartInfo(string args, string? workingDirectory)
@@ -43,15 +44,15 @@ private ProcessStartInfo MakeDotnetStartInfo(string args, string? workingDirecto
4344
private bool RunCommandAux(string args, string? workingDirectory, out IList<string> output, bool silent)
4445
{
4546
var dirLog = string.IsNullOrWhiteSpace(workingDirectory) ? "" : $" in {workingDirectory}";
46-
logger.LogInfo($"Running {Exec} {args}{dirLog}");
47+
logger.LogInfo($"Running '{Exec} {args}'{dirLog}");
4748
var pi = MakeDotnetStartInfo(args, workingDirectory);
4849
var threadId = Environment.CurrentManagedThreadId;
4950
void onOut(string s) => logger.Log(silent ? Severity.Debug : Severity.Info, s, threadId);
5051
void onError(string s) => logger.LogError(s, threadId);
5152
var exitCode = pi.ReadOutput(out output, onOut, onError);
5253
if (exitCode != 0)
5354
{
54-
logger.LogError($"Command {Exec} {args}{dirLog} failed with exit code {exitCode}");
55+
logger.LogError($"Command '{Exec} {args}'{dirLog} failed with exit code {exitCode}");
5556
return false;
5657
}
5758
return true;

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

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public class FileProvider
2121
private readonly Lazy<string[]> dlls;
2222
private readonly Lazy<string[]> nugetConfigs;
2323
private readonly Lazy<string[]> globalJsons;
24+
private readonly Lazy<string[]> packagesConfigs;
2425
private readonly Lazy<string[]> razorViews;
2526
private readonly Lazy<string[]> resources;
2627
private readonly Lazy<string?> rootNugetConfig;
@@ -32,31 +33,38 @@ public FileProvider(DirectoryInfo sourceDir, ILogger logger)
3233

3334
all = GetAllFiles();
3435
allNonBinary = new Lazy<FileInfo[]>(() => all.Where(f => !binaryFileExtensions.Contains(f.Extension.ToLowerInvariant())).ToArray());
35-
smallNonBinary = new Lazy<string[]>(() =>
36-
{
37-
var ret = SelectSmallFiles(allNonBinary.Value).SelectFileNames().ToArray();
38-
logger.LogInfo($"Found {ret.Length} small non-binary files in {SourceDir}.");
39-
return ret;
40-
});
36+
smallNonBinary = new Lazy<string[]>(() => ReturnAndLogFiles("small non-binary", SelectSmallFiles(allNonBinary.Value).SelectFileNames().ToArray()));
4137
sources = new Lazy<string[]>(() => SelectTextFileNamesByExtension("source", ".cs"));
4238
projects = new Lazy<string[]>(() => SelectTextFileNamesByExtension("project", ".csproj"));
4339
solutions = new Lazy<string[]>(() => SelectTextFileNamesByExtension("solution", ".sln"));
4440
dlls = new Lazy<string[]>(() => SelectBinaryFileNamesByExtension("DLL", ".dll"));
45-
nugetConfigs = new Lazy<string[]>(() => allNonBinary.Value.SelectFileNamesByName("nuget.config").ToArray());
46-
globalJsons = new Lazy<string[]>(() => allNonBinary.Value.SelectFileNamesByName("global.json").ToArray());
41+
nugetConfigs = new Lazy<string[]>(() => SelectTextFileNamesByName("nuget.config"));
42+
globalJsons = new Lazy<string[]>(() => SelectTextFileNamesByName("global.json"));
43+
packagesConfigs = new Lazy<string[]>(() => SelectTextFileNamesByName("packages.config"));
4744
razorViews = new Lazy<string[]>(() => SelectTextFileNamesByExtension("razor view", ".cshtml", ".razor"));
4845
resources = new Lazy<string[]>(() => SelectTextFileNamesByExtension("resource", ".resx"));
4946

5047
rootNugetConfig = new Lazy<string?>(() => all.SelectRootFiles(SourceDir).SelectFileNamesByName("nuget.config").FirstOrDefault());
5148
}
5249

53-
private string[] SelectTextFileNamesByExtension(string filetype, params string[] extensions)
50+
private string[] ReturnAndLogFiles(string filetype, IEnumerable<string> files)
5451
{
55-
var ret = allNonBinary.Value.SelectFileNamesByExtension(extensions).ToArray();
52+
var ret = files.ToArray();
5653
logger.LogInfo($"Found {ret.Length} {filetype} files in {SourceDir}.");
5754
return ret;
5855
}
5956

57+
private string[] SelectTextFileNamesByExtension(string filetype, params string[] extensions)
58+
=> ReturnAndLogFiles(filetype, allNonBinary.Value.SelectFileNamesByExtension(extensions));
59+
60+
private string[] SelectTextFileNamesByName(string name)
61+
{
62+
var ret = allNonBinary.Value.SelectFileNamesByName(name).ToArray();
63+
var ending = ret.Length == 0 ? "." : $": {string.Join(", ", ret.OrderBy(s => s))}.";
64+
logger.LogInfo($"Found {ret.Length} {name} files in {SourceDir}{ending}");
65+
return ret;
66+
}
67+
6068
private string[] SelectBinaryFileNamesByExtension(string filetype, params string[] extensions)
6169
{
6270
var ret = all.SelectFileNamesByExtension(extensions).ToArray();
@@ -117,6 +125,7 @@ private FileInfo[] GetAllFiles()
117125
public ICollection<string> NugetConfigs => nugetConfigs.Value;
118126
public string? RootNugetConfig => rootNugetConfig.Value;
119127
public IEnumerable<string> GlobalJsons => globalJsons.Value;
128+
public ICollection<string> PackagesConfigs => packagesConfigs.Value;
120129
public ICollection<string> RazorViews => razorViews.Value;
121130
public ICollection<string> Resources => resources.Value;
122131
}

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

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ internal class NugetExeWrapper : IDisposable
2020
/// <summary>
2121
/// The list of package files.
2222
/// </summary>
23-
private readonly FileInfo[] packageFiles;
23+
private readonly ICollection<string> packageFiles;
2424

25-
public int PackageCount => packageFiles.Length;
25+
public int PackageCount => packageFiles.Count;
2626

2727
private readonly string? backupNugetConfig;
2828
private readonly string? nugetConfigPath;
@@ -37,23 +37,21 @@ internal class NugetExeWrapper : IDisposable
3737
/// <summary>
3838
/// Create the package manager for a specified source tree.
3939
/// </summary>
40-
public NugetExeWrapper(string sourceDir, TemporaryDirectory packageDirectory, Util.Logging.ILogger logger)
40+
public NugetExeWrapper(FileProvider fileProvider, TemporaryDirectory packageDirectory, Util.Logging.ILogger logger)
4141
{
4242
this.packageDirectory = packageDirectory;
4343
this.logger = logger;
4444

45-
packageFiles = new DirectoryInfo(sourceDir)
46-
.EnumerateFiles("packages.config", SearchOption.AllDirectories)
47-
.ToArray();
45+
packageFiles = fileProvider.PackagesConfigs;
4846

49-
if (packageFiles.Length > 0)
47+
if (packageFiles.Count > 0)
5048
{
51-
logger.LogInfo($"Found {packageFiles.Length} packages.config files, trying to use nuget.exe for package restore");
52-
nugetExe = ResolveNugetExe(sourceDir);
49+
logger.LogInfo($"Found packages.config files, trying to use nuget.exe for package restore");
50+
nugetExe = ResolveNugetExe(fileProvider.SourceDir.FullName);
5351
if (HasNoPackageSource())
5452
{
5553
// We only modify or add a top level nuget.config file
56-
nugetConfigPath = Path.Combine(sourceDir, "nuget.config");
54+
nugetConfigPath = Path.Combine(fileProvider.SourceDir.FullName, "nuget.config");
5755
try
5856
{
5957
if (File.Exists(nugetConfigPath))
@@ -86,10 +84,6 @@ public NugetExeWrapper(string sourceDir, TemporaryDirectory packageDirectory, Ut
8684
}
8785
}
8886
}
89-
else
90-
{
91-
logger.LogInfo("Found no packages.config file");
92-
}
9387
}
9488

9589
/// <summary>
@@ -195,7 +189,7 @@ private bool TryRestoreNugetPackage(string package)
195189
/// </summary>
196190
public int InstallPackages()
197191
{
198-
return packageFiles.Count(package => TryRestoreNugetPackage(package.FullName));
192+
return packageFiles.Count(package => TryRestoreNugetPackage(package));
199193
}
200194

201195
private bool HasNoPackageSource()

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public HashSet<AssemblyLookupLocation> Restore()
105105
: [unresponsiveMissingPackageLocation];
106106
}
107107

108-
using (var nuget = new NugetExeWrapper(fileProvider.SourceDir.FullName, legacyPackageDirectory, logger))
108+
using (var nuget = new NugetExeWrapper(fileProvider, legacyPackageDirectory, logger))
109109
{
110110
var count = nuget.InstallPackages();
111111

@@ -178,7 +178,7 @@ private List<string> GetReachableFallbackNugetFeeds()
178178
logger.LogInfo($"No fallback Nuget feeds specified. Using default feed: {PublicNugetOrgFeed}");
179179
}
180180

181-
logger.LogInfo($"Checking fallback Nuget feed reachability on feeds: {string.Join(", ", fallbackFeeds.OrderBy(f => f))}");
181+
logger.LogInfo($"Checking fallback Nuget feed reachability on feeds: {string.Join(", ", fallbackFeeds.OrderBy(f => f))}");
182182
var (initialTimeout, tryCount) = GetFeedRequestSettings(isFallback: true);
183183
var reachableFallbackFeeds = fallbackFeeds.Where(feed => IsFeedReachable(feed, initialTimeout, tryCount, allowExceptions: false)).ToList();
184184
if (reachableFallbackFeeds.Count == 0)

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,17 +121,20 @@ public void Started(int item, int total, string source)
121121

122122
public void MissingType(string type)
123123
{
124-
logger.Log(Severity.Debug, "Missing type {0}", type);
124+
logger.LogDebug($"Missing type {type}");
125125
}
126126

127127
public void MissingNamespace(string @namespace)
128128
{
129-
logger.Log(Severity.Info, "Missing namespace {0}", @namespace);
129+
logger.LogInfo($"Missing namespace {@namespace}");
130130
}
131131

132132
public void MissingSummary(int missingTypes, int missingNamespaces)
133133
{
134-
logger.Log(Severity.Info, "Failed to resolve {0} types in {1} namespaces", missingTypes, missingNamespaces);
134+
if (missingTypes > 0 || missingNamespaces > 0)
135+
{
136+
logger.LogInfo($"Failed to resolve {missingTypes} types in {missingNamespaces} namespaces");
137+
}
135138
}
136139
}
137140

0 commit comments

Comments
 (0)