Skip to content

Commit 0b59a98

Browse files
committed
Replace MessageSeverity with LogLevel, add Trace level
1 parent f19e8ab commit 0b59a98

18 files changed

+207
-202
lines changed

src/BuiltInTools/dotnet-watch/CommandLine/CommandLineOptions.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,15 +170,20 @@ internal sealed class CommandLineOptions
170170

171171
var targetFrameworkOption = (Option<string>?)buildOptions.SingleOrDefault(option => option.Name == "--framework");
172172

173+
var logLevel = parseResult.GetValue(verboseOption)
174+
? LogLevel.Debug
175+
: parseResult.GetValue(quietOption)
176+
? LogLevel.Warning
177+
: LogLevel.Information;
178+
173179
return new()
174180
{
175181
List = parseResult.GetValue(listOption),
176182
GlobalOptions = new()
177183
{
178-
Quiet = parseResult.GetValue(quietOption),
184+
LogLevel = logLevel,
179185
NoHotReload = parseResult.GetValue(noHotReloadOption),
180186
NonInteractive = parseResult.GetValue(NonInteractiveOption),
181-
Verbose = parseResult.GetValue(verboseOption),
182187
BinaryLogPath = ParseBinaryLogFilePath(binLogPath),
183188
},
184189

src/BuiltInTools/dotnet-watch/CommandLine/EnvironmentOptions.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System.Diagnostics;
5+
using Microsoft.Extensions.Logging;
56

67
namespace Microsoft.DotNet.Watch
78
{
@@ -12,16 +13,21 @@ internal enum TestFlags
1213
RunningAsTest = 1 << 0,
1314
MockBrowser = 1 << 1,
1415

16+
/// <summary>
17+
/// Elevates the logging level
18+
/// </summary>
19+
TraceLogging = 1 << 2,
20+
1521
/// <summary>
1622
/// Instead of using <see cref="Console.ReadKey()"/> to watch for Ctrl+C, Ctlr+R, and other keys, read from standard input.
1723
/// This allows tests to trigger key based events.
1824
/// </summary>
19-
ReadKeyFromStdin = 1 << 2,
25+
ReadKeyFromStdin = 1 << 3,
2026

2127
/// <summary>
2228
/// Redirects the output of the launched browser process to watch output.
2329
/// </summary>
24-
RedirectBrowserOutput = 1 << 3,
30+
RedirectBrowserOutput = 1 << 4,
2531
}
2632

2733
internal sealed record EnvironmentOptions(
@@ -35,6 +41,7 @@ internal sealed record EnvironmentOptions(
3541
bool SuppressBrowserRefresh = false,
3642
bool SuppressEmojis = false,
3743
bool RestartOnRudeEdit = false,
44+
LogLevel? CliLogLevel = null,
3845
string? AutoReloadWebSocketHostName = null,
3946
int? AutoReloadWebSocketPort = null,
4047
string? BrowserPath = null,
@@ -53,6 +60,7 @@ internal sealed record EnvironmentOptions(
5360
SuppressBrowserRefresh: EnvironmentVariables.SuppressBrowserRefresh,
5461
SuppressEmojis: EnvironmentVariables.SuppressEmojis,
5562
RestartOnRudeEdit: EnvironmentVariables.RestartOnRudeEdit,
63+
CliLogLevel: EnvironmentVariables.CliLogLevel,
5664
AutoReloadWebSocketHostName: EnvironmentVariables.AutoReloadWSHostName,
5765
AutoReloadWebSocketPort: EnvironmentVariables.AutoReloadWSPort,
5866
BrowserPath: EnvironmentVariables.BrowserPath,

src/BuiltInTools/dotnet-watch/CommandLine/EnvironmentVariables.cs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using Microsoft.Extensions.Logging;
5+
46
namespace Microsoft.DotNet.Watch;
57

68
internal static class EnvironmentVariables
@@ -20,7 +22,19 @@ public static class Names
2022
public const string SuppressBrowserRefresh = "DOTNET_WATCH_SUPPRESS_BROWSER_REFRESH";
2123
}
2224

23-
public static bool VerboseCliOutput => ReadBool("DOTNET_CLI_CONTEXT_VERBOSE");
25+
public static LogLevel? CliLogLevel
26+
{
27+
get
28+
{
29+
var value = Environment.GetEnvironmentVariable("DOTNET_CLI_CONTEXT_VERBOSE");
30+
return string.Equals(value, "trace", StringComparison.OrdinalIgnoreCase)
31+
? LogLevel.Trace
32+
: ParseBool(value)
33+
? LogLevel.Debug
34+
: null;
35+
}
36+
}
37+
2438
public static bool IsPollingEnabled => ReadBool("DOTNET_USE_POLLING_FILE_WATCHER");
2539
public static bool SuppressEmojis => ReadBool("DOTNET_WATCH_SUPPRESS_EMOJIS");
2640
public static bool RestartOnRudeEdit => ReadBool("DOTNET_WATCH_RESTART_ON_RUDE_EDIT");
@@ -46,11 +60,14 @@ public static class Names
4660
public static string? BrowserPath => Environment.GetEnvironmentVariable("DOTNET_WATCH_BROWSER_PATH");
4761

4862
private static bool ReadBool(string variableName)
49-
=> Environment.GetEnvironmentVariable(variableName) is var value && (value == "1" || bool.TryParse(value, out var boolValue) && boolValue);
63+
=> ParseBool(Environment.GetEnvironmentVariable(variableName));
5064

5165
private static TimeSpan? ReadTimeSpan(string variableName)
5266
=> Environment.GetEnvironmentVariable(variableName) is var value && long.TryParse(value, out var intValue) && intValue >= 0 ? TimeSpan.FromMilliseconds(intValue) : null;
5367

5468
private static int? ReadInt(string variableName)
5569
=> Environment.GetEnvironmentVariable(variableName) is var value && int.TryParse(value, out var intValue) ? intValue : null;
70+
71+
private static bool ParseBool(string? value)
72+
=> value == "1" || bool.TryParse(value, out var boolValue) && boolValue;
5673
}

src/BuiltInTools/dotnet-watch/CommandLine/GlobalOptions.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using Microsoft.Extensions.Logging;
5+
46
namespace Microsoft.DotNet.Watch;
57

68
internal sealed class GlobalOptions
79
{
8-
public bool Quiet { get; init; }
9-
public bool Verbose { get; init; }
10+
public LogLevel LogLevel { get; init; }
1011
public bool NoHotReload { get; init; }
1112
public bool NonInteractive { get; init; }
1213

src/BuiltInTools/dotnet-watch/HotReload/AppModels/WebApplicationAppModel.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,13 @@ public bool IsServerSupported(ProjectGraphNode projectNode, ILogger logger)
7171
{
7272
if (context.EnvironmentOptions.SuppressBrowserRefresh)
7373
{
74-
logger.Log(MessageDescriptor.SkippingConfiguringBrowserRefresh_SuppressedViaEnvironmentVariable.WithSeverityWhen(MessageSeverity.Error, RequiresBrowserRefresh), EnvironmentVariables.Names.SuppressBrowserRefresh);
74+
logger.Log(MessageDescriptor.SkippingConfiguringBrowserRefresh_SuppressedViaEnvironmentVariable.WithLevelWhen(LogLevel.Error, RequiresBrowserRefresh), EnvironmentVariables.Names.SuppressBrowserRefresh);
7575
return false;
7676
}
7777

7878
if (!projectNode.IsNetCoreApp(minVersion: s_minimumSupportedVersion))
7979
{
80-
logger.Log(MessageDescriptor.SkippingConfiguringBrowserRefresh_TargetFrameworkNotSupported.WithSeverityWhen(MessageSeverity.Error, RequiresBrowserRefresh));
80+
logger.Log(MessageDescriptor.SkippingConfiguringBrowserRefresh_TargetFrameworkNotSupported.WithLevelWhen(LogLevel.Error, RequiresBrowserRefresh));
8181
return false;
8282
}
8383

src/BuiltInTools/dotnet-watch/HotReload/CompilationHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -521,7 +521,7 @@ void ReportDiagnostic(Diagnostic diagnostic, MessageDescriptor descriptor, strin
521521
{
522522
errorsToDisplayInApp.Add(MessageDescriptor.RestartingApplicationToApplyChanges.GetMessage());
523523
}
524-
else if (descriptor.Severity != MessageSeverity.None)
524+
else if (descriptor.Level != LogLevel.None)
525525
{
526526
errorsToDisplayInApp.Add(descriptor.GetMessage(args));
527527
}

src/BuiltInTools/dotnet-watch/HotReload/HotReloadDotNetWatcher.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public HotReloadDotNetWatcher(DotNetWatchContext context, IConsole console, IRun
3030
_runtimeProcessLauncherFactory = runtimeProcessLauncherFactory;
3131
if (!context.Options.NonInteractive)
3232
{
33-
var consoleInput = new ConsoleInputReader(_console, context.Options.Quiet, context.EnvironmentOptions.SuppressEmojis);
33+
var consoleInput = new ConsoleInputReader(_console, context.Options.LogLevel, context.EnvironmentOptions.SuppressEmojis);
3434

3535
var noPrompt = context.EnvironmentOptions.RestartOnRudeEdit;
3636
if (noPrompt)

src/BuiltInTools/dotnet-watch/HotReload/IncrementalMSBuildWorkspace.cs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using Microsoft.CodeAnalysis.MSBuild;
1010
using Microsoft.CodeAnalysis.Text;
1111
using Microsoft.Extensions.Logging;
12+
using Windows.Win32.Security.Cryptography;
1213

1314
namespace Microsoft.DotNet.Watch;
1415

@@ -230,16 +231,13 @@ private Task UpdateSolutionAsync(Solution newSolution, string operationDisplayNa
230231

231232
private async Task ReportSolutionFilesAsync(Solution solution, int updateId, string operationDisplayName, CancellationToken cancellationToken)
232233
{
233-
#if DEBUG
234-
_logger.LogDebug("Solution: {Path}", solution.FilePath);
234+
_logger.LogDebug("Solution after {Operation}: v{Version}", operationDisplayName, updateId);
235235

236-
if (!_logger.IsEnabled(LogLevel.Debug))
236+
if (!_logger.IsEnabled(LogLevel.Trace))
237237
{
238238
return;
239239
}
240240

241-
_logger.LogDebug("Solution after {Operation}: v{Version}", operationDisplayName, updateId);
242-
243241
foreach (var project in solution.Projects)
244242
{
245243
_logger.LogDebug(" Project: {Path}", project.FilePath);
@@ -265,8 +263,5 @@ async ValueTask InspectDocumentAsync(TextDocument document, string kind)
265263
var text = await document.GetTextAsync(cancellationToken);
266264
_logger.LogDebug(" {Kind}: {FilePath} [{Checksum}]", kind, document.FilePath, Convert.ToBase64String(text.GetChecksum().ToArray()));
267265
}
268-
#else
269-
await Task.CompletedTask;
270-
#endif
271266
}
272267
}

src/BuiltInTools/dotnet-watch/Program.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ public static async Task<int> Main(string[] args)
4646
args,
4747
new PhysicalConsole(environmentOptions.TestFlags),
4848
environmentOptions,
49-
EnvironmentVariables.VerboseCliOutput,
5049
out var exitCode);
5150

5251
if (program == null)
@@ -64,18 +63,19 @@ public static async Task<int> Main(string[] args)
6463
}
6564
}
6665

67-
private static Program? TryCreate(IReadOnlyList<string> args, IConsole console, EnvironmentOptions environmentOptions, bool verbose, out int errorCode)
66+
private static Program? TryCreate(IReadOnlyList<string> args, IConsole console, EnvironmentOptions environmentOptions, out int errorCode)
6867
{
69-
var parsingLoggerFactory = new LoggerFactory(new ConsoleReporter(console, verbose, quiet: false, environmentOptions.SuppressEmojis));
68+
var parsingLoggerFactory = new LoggerFactory(new ConsoleReporter(console, environmentOptions.SuppressEmojis), environmentOptions.CliLogLevel ?? LogLevel.Information);
7069
var options = CommandLineOptions.Parse(args, parsingLoggerFactory.CreateLogger(LogComponentName), console.Out, out errorCode);
7170
if (options == null)
7271
{
7372
// an error reported or help printed:
7473
return null;
7574
}
7675

77-
var reporter = new ConsoleReporter(console, verbose || options.GlobalOptions.Verbose, options.GlobalOptions.Quiet, environmentOptions.SuppressEmojis);
78-
var loggerFactory = new LoggerFactory(reporter);
76+
var logLevel = environmentOptions.CliLogLevel ?? options.GlobalOptions.LogLevel;
77+
var reporter = new ConsoleReporter(console, environmentOptions.SuppressEmojis);
78+
var loggerFactory = new LoggerFactory(reporter, logLevel);
7979
return TryCreate(options, console, environmentOptions, loggerFactory, reporter, out errorCode);
8080
}
8181

src/BuiltInTools/dotnet-watch/UI/ConsoleInputReader.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using Microsoft.Extensions.Logging;
5+
46
namespace Microsoft.DotNet.Watch
57
{
6-
internal sealed class ConsoleInputReader(IConsole console, bool quiet, bool suppressEmojis)
8+
internal sealed class ConsoleInputReader(IConsole console, LogLevel logLevel, bool suppressEmojis)
79
{
810
private readonly object _writeLock = new();
911

1012
public async Task<ConsoleKey> GetKeyAsync(string prompt, Func<ConsoleKeyInfo, bool> validateInput, CancellationToken cancellationToken)
1113
{
12-
if (quiet)
14+
if (logLevel > LogLevel.Information)
1315
{
1416
return ConsoleKey.Escape;
1517
}

0 commit comments

Comments
 (0)