-
Notifications
You must be signed in to change notification settings - Fork 74
Create and use console reporter API #237
Conversation
095613b
to
bb051ed
Compare
Should resolve #227. The formatting: dotnet-watch prefixes all its lines with Some examples of output:
cc @CesarBS |
using (var assemblyStream = GetType().GetTypeInfo().Assembly.GetManifestResourceStream("dotnetwatch.targets")) | ||
using ( | ||
var assemblyStream = | ||
GetType().GetTypeInfo().Assembly.GetManifestResourceStream("dotnetwatch.targets")) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: actually prefer the previous format. At least move the var
line inline with using
.
.Verbose(f => f.WithColor(ConsoleColor.DarkGray)) | ||
.Warn(f => f.WithColor(ConsoleColor.Yellow)) | ||
.Error(f => f.WithColor(ConsoleColor.Red)) | ||
.Build(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't this use the same logic used in CreateReporter
in the watcher?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This tool doesn't actually have --verbose
flags. Though I suppose detecting output redirection for colorless output is still valid...although probably a very unlikely usage.
@@ -86,7 +95,7 @@ public int Run(string[] args) | |||
} | |||
catch (Exception exception) | |||
{ | |||
_logger.LogCritical("An error occurred. {ErrorMessage}", exception.Message); | |||
_reporter.Error("An error occurred. " + exception.Message); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: use string interpolation instead of +
ex.Message); | ||
_reporter.Error( | ||
$"An error occurred while trying to create the table and index. {ex.Message}" | ||
); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: move );
to line above
if (!_console.IsOutputRedirected) | ||
{ | ||
f.WithColor(ConsoleColor.DarkGray); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What if it is redirected?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's output is colorless. Users might pipe commands to a file. We want to avoid spitting out the ANSI color codes.
public static bool IsGlobalVerbose() | ||
{ | ||
bool globalVerbose; | ||
bool.TryParse(Environment.GetEnvironmentVariable("DOTNET_CLI_CONTEXT_VERBOSE"), out globalVerbose); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A lot of environment variables work with true
and 1
. Shouldn't you check that here? Or do the docs instruct people to only set this to true
if they want to enable it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it's always the result of bool.ToString()
. cref https://github.com/dotnet/cli/blob/rel/1.0.0/src/dotnet/Program.cs#L164
|
||
namespace Microsoft.Extensions.Tools.Internal | ||
{ | ||
public class ColorFormatter : IFormatter |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't we have similar logic in the console logger already?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah. It has an IConsole
API internally. The API was insufficient because it didn't provide access to Console.In
.
Follow up: we could remove duplication by putting this API and others in a common location to be used by Logging.Console. Could help with aspnet/Logging#491
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah it would be good to consolidate these things in a single place. Can you open an issue to track this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
public void Error(string message) | ||
=> Write(_console.Error, _error.Format(message)); | ||
|
||
private void Write(TextWriter writer, string message) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're not using writer
here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add tests to verify each of the methods that call this are writing to the expected stream.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh wow, yeah, dumb mistake.
try | ||
{ | ||
var description = "Creates table and indexes in Microsoft SQL Server database " + | ||
"to be used for distributed caching"; | ||
"to be used for distributed caching"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: make it a single string.
Just approved, but: are you adding the tests to make sure each level is writing to the expected stream (out or err)? |
} | ||
|
||
[Fact] | ||
public void WritesToStandardStreams() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@CesarBS Added it :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
when tests pass.
The reporter API breaks down complex formatter in to composable compontents. Adds 'ReporterBuilder' and 'FormatterBuilder' as syntax sugar for creating reporters with complex formatting rules Changes in dotnet-watch - Remove dependency on Microsoft.Extensions.Logging and instead use IReporter for console output - Only use color output when stdout and stderr are not being redirected - Make the default output less noisy Changes in dotnet-user-secrets - Remove dependency on Microsoft.Extensions.Logging to use IReporter Changes in dotnet-sql-cache - Remove dependency on Microsoft.Extensions.Logging to use IReporter - Add --verbose option
7bcad6b
to
17da524
Compare
The reporter API breaks down complex formatter in to composable compontents. Adds 'ReporterBuilder' and 'FormatterBuilder' as syntax sugar for creating reporters with complex formatting rules
Changes:
Resolves #227
cc @muratg