Skip to content
This repository was archived by the owner on Nov 20, 2018. It is now read-only.

Create and use console reporter API #237

Merged
merged 1 commit into from
Dec 1, 2016
Merged

Conversation

natemcmaster
Copy link
Contributor

@natemcmaster natemcmaster commented Nov 23, 2016

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:

  • 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
  • dotnet-watch: Make the default output less noisy

Resolves #227

cc @muratg

@natemcmaster natemcmaster force-pushed the namc/reporter branch 2 times, most recently from 095613b to bb051ed Compare November 29, 2016 20:25
@natemcmaster
Copy link
Contributor Author

Should resolve #227.

The formatting:
verbose = dark gray, only appears when --verbose specified
output = normal, white text. Does not appear if --quiet specified.
warn = yellow text
error = red text

dotnet-watch prefixes all its lines with watch : to distinguish from the sub-process's output.

Some examples of output:
dotnet watch run
image
dotnet watch --verbose run
image

dotnet user-secrets list --verbose
image

dotnet user-secret list --verbose with errors
image

cc @CesarBS

using (var assemblyStream = GetType().GetTypeInfo().Assembly.GetManifestResourceStream("dotnetwatch.targets"))
using (
var assemblyStream =
GetType().GetTypeInfo().Assembly.GetManifestResourceStream("dotnetwatch.targets"))
Copy link
Contributor

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();
Copy link
Contributor

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?

Copy link
Contributor Author

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);
Copy link
Contributor

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}"
);
Copy link
Contributor

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);
}
Copy link
Contributor

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?

Copy link
Contributor Author

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);
Copy link
Contributor

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?

Copy link
Contributor Author

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
Copy link
Contributor

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?

Copy link
Contributor Author

@natemcmaster natemcmaster Nov 30, 2016

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

Copy link
Contributor

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?

Copy link
Contributor Author

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)
Copy link
Contributor

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.

Copy link
Contributor

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.

Copy link
Contributor Author

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";
Copy link
Contributor

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.

@cesarblum
Copy link
Contributor

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()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@CesarBS Added it :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:shipit: 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
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants