Skip to content

Commit ddfed6e

Browse files
committed
C#: Changing logging of dotnet commands to Debug except for dotnet --info and friends.
1 parent 72ffcf5 commit ddfed6e

File tree

4 files changed

+24
-21
lines changed

4 files changed

+24
-21
lines changed

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.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)