未验证 提交 44157c30 编写于 作者: J Joey Robichaud 提交者: GitHub

Merge pull request #31622 from JoeRobich/fewer-warnings

[dotnet-format] Decreased the logged severity of workspace diagnostic warnings
......@@ -21,12 +21,15 @@ namespace Microsoft.CodeAnalysis.Tools.CodeFormatter
{
internal static class CodeFormatter
{
public static async Task<int> FormatWorkspaceAsync(ILogger logger, string solutionOrProjectPath, bool isSolution, CancellationToken cancellationToken)
const int MaxLoggedWorkspaceWarnings = 5;
public static async Task<int> FormatWorkspaceAsync(ILogger logger, string solutionOrProjectPath, bool isSolution, bool logAllWorkspaceWarnings, CancellationToken cancellationToken)
{
logger.LogInformation(string.Format(Resources.Formatting_code_files_in_workspace_0, solutionOrProjectPath));
logger.LogTrace(Resources.Loading_workspace);
var loggedWarningCount = 0;
var exitCode = 1;
var workspaceStopwatch = Stopwatch.StartNew();
......@@ -42,14 +45,7 @@ public static async Task<int> FormatWorkspaceAsync(ILogger logger, string soluti
using (var workspace = MSBuildWorkspace.Create(properties))
{
workspace.WorkspaceFailed += (s, e) =>
{
if (e.Diagnostic.Kind != WorkspaceDiagnosticKind.Failure)
{
logger.LogError(e.Diagnostic.Message);
logger.LogError(Resources.Unable_to_load_workspace);
}
};
workspace.WorkspaceFailed += LogWorkspaceWarnings;
var projectPath = string.Empty;
if (isSolution)
......@@ -73,6 +69,27 @@ public static async Task<int> FormatWorkspaceAsync(ILogger logger, string soluti
logger.LogInformation(Resources.Format_complete);
return exitCode;
void LogWorkspaceWarnings(object sender, WorkspaceDiagnosticEventArgs args)
{
if (args.Diagnostic.Kind == WorkspaceDiagnosticKind.Failure)
{
return;
}
logger.LogWarning(args.Diagnostic.Message);
if (!logAllWorkspaceWarnings)
{
loggedWarningCount++;
if (loggedWarningCount == MaxLoggedWorkspaceWarnings)
{
logger.LogWarning(Resources.Maximum_number_of_workspace_warnings_to_log_has_been_reached_Set_the_verbosity_option_to_the_diagnostic_level_to_see_all_warnings);
((MSBuildWorkspace)sender).WorkspaceFailed -= LogWorkspaceWarnings;
}
}
}
}
private static async Task<int> FormatFilesInWorkspaceAsync(ILogger logger, Workspace workspace, string projectPath, ICodingConventionsManager codingConventionsManager, CancellationToken cancellationToken)
......
......@@ -63,7 +63,8 @@ public static async Task<int> Run(string workspace, string verbosity, IConsole c
MSBuildEnvironment.ApplyEnvironmentVariables(workspaceDirectory);
MSBuildCoreLoader.LoadDotnetInstance(workspaceDirectory);
return await CodeFormatter.FormatWorkspaceAsync(logger, workspacePath, isSolution, cancellationTokenSource.Token).ConfigureAwait(false);
return await CodeFormatter.FormatWorkspaceAsync(
logger, workspacePath, isSolution, logAllWorkspaceWarnings: logLevel == LogLevel.Trace, cancellationTokenSource.Token).ConfigureAwait(false);
}
catch (FileNotFoundException fex)
{
......
......@@ -2,8 +2,20 @@
=============
`dotnet-format` is a code formatter for `dotnet` that applies style preferences to a project or solution. Preferences will be read from an `.editorconfig` file, if present, otherwise a default set of preferences will be used.
### How To Install
The `dotnet-format` nuget package is currently being hosted on myget. You can visit the [dotnet-format myget page](https://dotnet.myget.org/feed/roslyn/package/nuget/dotnet-format) to get the latest version number.
You can install the tool using the following command.
```console
dotnet tool install -g dotnet-format --version <version> --add-source https://dotnet.myget.org/F/roslyn/api/v3/index.json
```
### How To Use
By default `dotnet-format` will look in the current directory for a project or solution file and use that as the workspace to format. If more than one project or solution file is present in the current directory you will need to specify the workspace to format using the `-w` option. You can control how verbose the output will be by using the `-v` option.
```
Usage:
dotnet-format [options]
......@@ -21,11 +33,19 @@ Add `format` after `dotnet` and before the command arguments that you want to ru
| Examples |
| -------------------------------------------------------- |
| dotnet **format** |
| dotnet **format** &lt;workspace&gt; |
| dotnet **format** -w &lt;workspace&gt; |
| dotnet **format** -v diag |
| dotnet **format** -w &lt;workspace&gt; -v diag |
### How To Uninstall
You can uninstall the tool using the following command.
## Build the Tool from source
```console
dotnet tool uninstall -g dotnet-format
```
### How To Build From Source
You can build and package the tool using the following commands. The instructions assume that you are in the root of the repository.
......@@ -42,9 +62,3 @@ dotnet format
```
> Note: On macOS and Linux, `.\nupkg` will need be switched to `./nupkg` to accomodate for the different slash directions.
You can uninstall the tool using the following command.
```console
dotnet tool uninstall -g dotnet-format
```
......@@ -153,6 +153,16 @@ internal class Resources {
}
}
/// <summary>
/// Looks up a localized string similar to Maximum number of workspace warnings to log has been reached. Set the --verbosity option to the &apos;diagnostic&apos; level to see all warnings..
/// </summary>
internal static string Maximum_number_of_workspace_warnings_to_log_has_been_reached_Set_the_verbosity_option_to_the_diagnostic_level_to_see_all_warnings {
get {
return ResourceManager.GetString("Maximum_number_of_workspace_warnings_to_log_has_been_reached_Set_the_verbosity_op" +
"tion_to_the_diagnostic_level_to_see_all_warnings", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Multiple MSBuild project files found in &apos;{0}&apos;. Specify which to use with the --workspace option..
/// </summary>
......
......@@ -177,4 +177,7 @@
<data name="The_solution_or_project_file_to_operate_on_If_a_file_is_not_specified_the_command_will_search_the_current_directory_for_one" xml:space="preserve">
<value>The solution or project file to operate on. If a file is not specified, the command will search the current directory for one.</value>
</data>
<data name="Maximum_number_of_workspace_warnings_to_log_has_been_reached_Set_the_verbosity_option_to_the_diagnostic_level_to_see_all_warnings" xml:space="preserve">
<value>Maximum number of workspace warnings to log has been reached. Set the --verbosity option to the 'diagnostic' level to see all warnings.</value>
</data>
</root>
\ No newline at end of file
......@@ -52,6 +52,11 @@
<target state="new">Loading workspace.</target>
<note />
</trans-unit>
<trans-unit id="Maximum_number_of_workspace_warnings_to_log_has_been_reached_Set_the_verbosity_option_to_the_diagnostic_level_to_see_all_warnings">
<source>Maximum number of workspace warnings to log has been reached. Set the --verbosity option to the 'diagnostic' level to see all warnings.</source>
<target state="new">Maximum number of workspace warnings to log has been reached. Set the --verbosity option to the 'diagnostic' level to see all warnings.</target>
<note />
</trans-unit>
<trans-unit id="Multiple_MSBuild_project_files_found_in_0_Specify_which_to_use_with_the_workspace_option">
<source>Multiple MSBuild project files found in '{0}'. Specify which to use with the --workspace option.</source>
<target state="new">Multiple MSBuild project files found in '{0}'. Specify which to use with the --workspace option.</target>
......
......@@ -52,6 +52,11 @@
<target state="new">Loading workspace.</target>
<note />
</trans-unit>
<trans-unit id="Maximum_number_of_workspace_warnings_to_log_has_been_reached_Set_the_verbosity_option_to_the_diagnostic_level_to_see_all_warnings">
<source>Maximum number of workspace warnings to log has been reached. Set the --verbosity option to the 'diagnostic' level to see all warnings.</source>
<target state="new">Maximum number of workspace warnings to log has been reached. Set the --verbosity option to the 'diagnostic' level to see all warnings.</target>
<note />
</trans-unit>
<trans-unit id="Multiple_MSBuild_project_files_found_in_0_Specify_which_to_use_with_the_workspace_option">
<source>Multiple MSBuild project files found in '{0}'. Specify which to use with the --workspace option.</source>
<target state="new">Multiple MSBuild project files found in '{0}'. Specify which to use with the --workspace option.</target>
......
......@@ -52,6 +52,11 @@
<target state="new">Loading workspace.</target>
<note />
</trans-unit>
<trans-unit id="Maximum_number_of_workspace_warnings_to_log_has_been_reached_Set_the_verbosity_option_to_the_diagnostic_level_to_see_all_warnings">
<source>Maximum number of workspace warnings to log has been reached. Set the --verbosity option to the 'diagnostic' level to see all warnings.</source>
<target state="new">Maximum number of workspace warnings to log has been reached. Set the --verbosity option to the 'diagnostic' level to see all warnings.</target>
<note />
</trans-unit>
<trans-unit id="Multiple_MSBuild_project_files_found_in_0_Specify_which_to_use_with_the_workspace_option">
<source>Multiple MSBuild project files found in '{0}'. Specify which to use with the --workspace option.</source>
<target state="new">Multiple MSBuild project files found in '{0}'. Specify which to use with the --workspace option.</target>
......
......@@ -52,6 +52,11 @@
<target state="new">Loading workspace.</target>
<note />
</trans-unit>
<trans-unit id="Maximum_number_of_workspace_warnings_to_log_has_been_reached_Set_the_verbosity_option_to_the_diagnostic_level_to_see_all_warnings">
<source>Maximum number of workspace warnings to log has been reached. Set the --verbosity option to the 'diagnostic' level to see all warnings.</source>
<target state="new">Maximum number of workspace warnings to log has been reached. Set the --verbosity option to the 'diagnostic' level to see all warnings.</target>
<note />
</trans-unit>
<trans-unit id="Multiple_MSBuild_project_files_found_in_0_Specify_which_to_use_with_the_workspace_option">
<source>Multiple MSBuild project files found in '{0}'. Specify which to use with the --workspace option.</source>
<target state="new">Multiple MSBuild project files found in '{0}'. Specify which to use with the --workspace option.</target>
......
......@@ -52,6 +52,11 @@
<target state="new">Loading workspace.</target>
<note />
</trans-unit>
<trans-unit id="Maximum_number_of_workspace_warnings_to_log_has_been_reached_Set_the_verbosity_option_to_the_diagnostic_level_to_see_all_warnings">
<source>Maximum number of workspace warnings to log has been reached. Set the --verbosity option to the 'diagnostic' level to see all warnings.</source>
<target state="new">Maximum number of workspace warnings to log has been reached. Set the --verbosity option to the 'diagnostic' level to see all warnings.</target>
<note />
</trans-unit>
<trans-unit id="Multiple_MSBuild_project_files_found_in_0_Specify_which_to_use_with_the_workspace_option">
<source>Multiple MSBuild project files found in '{0}'. Specify which to use with the --workspace option.</source>
<target state="new">Multiple MSBuild project files found in '{0}'. Specify which to use with the --workspace option.</target>
......
......@@ -52,6 +52,11 @@
<target state="new">Loading workspace.</target>
<note />
</trans-unit>
<trans-unit id="Maximum_number_of_workspace_warnings_to_log_has_been_reached_Set_the_verbosity_option_to_the_diagnostic_level_to_see_all_warnings">
<source>Maximum number of workspace warnings to log has been reached. Set the --verbosity option to the 'diagnostic' level to see all warnings.</source>
<target state="new">Maximum number of workspace warnings to log has been reached. Set the --verbosity option to the 'diagnostic' level to see all warnings.</target>
<note />
</trans-unit>
<trans-unit id="Multiple_MSBuild_project_files_found_in_0_Specify_which_to_use_with_the_workspace_option">
<source>Multiple MSBuild project files found in '{0}'. Specify which to use with the --workspace option.</source>
<target state="new">Multiple MSBuild project files found in '{0}'. Specify which to use with the --workspace option.</target>
......
......@@ -52,6 +52,11 @@
<target state="new">Loading workspace.</target>
<note />
</trans-unit>
<trans-unit id="Maximum_number_of_workspace_warnings_to_log_has_been_reached_Set_the_verbosity_option_to_the_diagnostic_level_to_see_all_warnings">
<source>Maximum number of workspace warnings to log has been reached. Set the --verbosity option to the 'diagnostic' level to see all warnings.</source>
<target state="new">Maximum number of workspace warnings to log has been reached. Set the --verbosity option to the 'diagnostic' level to see all warnings.</target>
<note />
</trans-unit>
<trans-unit id="Multiple_MSBuild_project_files_found_in_0_Specify_which_to_use_with_the_workspace_option">
<source>Multiple MSBuild project files found in '{0}'. Specify which to use with the --workspace option.</source>
<target state="new">Multiple MSBuild project files found in '{0}'. Specify which to use with the --workspace option.</target>
......
......@@ -52,6 +52,11 @@
<target state="new">Loading workspace.</target>
<note />
</trans-unit>
<trans-unit id="Maximum_number_of_workspace_warnings_to_log_has_been_reached_Set_the_verbosity_option_to_the_diagnostic_level_to_see_all_warnings">
<source>Maximum number of workspace warnings to log has been reached. Set the --verbosity option to the 'diagnostic' level to see all warnings.</source>
<target state="new">Maximum number of workspace warnings to log has been reached. Set the --verbosity option to the 'diagnostic' level to see all warnings.</target>
<note />
</trans-unit>
<trans-unit id="Multiple_MSBuild_project_files_found_in_0_Specify_which_to_use_with_the_workspace_option">
<source>Multiple MSBuild project files found in '{0}'. Specify which to use with the --workspace option.</source>
<target state="new">Multiple MSBuild project files found in '{0}'. Specify which to use with the --workspace option.</target>
......
......@@ -52,6 +52,11 @@
<target state="new">Loading workspace.</target>
<note />
</trans-unit>
<trans-unit id="Maximum_number_of_workspace_warnings_to_log_has_been_reached_Set_the_verbosity_option_to_the_diagnostic_level_to_see_all_warnings">
<source>Maximum number of workspace warnings to log has been reached. Set the --verbosity option to the 'diagnostic' level to see all warnings.</source>
<target state="new">Maximum number of workspace warnings to log has been reached. Set the --verbosity option to the 'diagnostic' level to see all warnings.</target>
<note />
</trans-unit>
<trans-unit id="Multiple_MSBuild_project_files_found_in_0_Specify_which_to_use_with_the_workspace_option">
<source>Multiple MSBuild project files found in '{0}'. Specify which to use with the --workspace option.</source>
<target state="new">Multiple MSBuild project files found in '{0}'. Specify which to use with the --workspace option.</target>
......
......@@ -52,6 +52,11 @@
<target state="new">Loading workspace.</target>
<note />
</trans-unit>
<trans-unit id="Maximum_number_of_workspace_warnings_to_log_has_been_reached_Set_the_verbosity_option_to_the_diagnostic_level_to_see_all_warnings">
<source>Maximum number of workspace warnings to log has been reached. Set the --verbosity option to the 'diagnostic' level to see all warnings.</source>
<target state="new">Maximum number of workspace warnings to log has been reached. Set the --verbosity option to the 'diagnostic' level to see all warnings.</target>
<note />
</trans-unit>
<trans-unit id="Multiple_MSBuild_project_files_found_in_0_Specify_which_to_use_with_the_workspace_option">
<source>Multiple MSBuild project files found in '{0}'. Specify which to use with the --workspace option.</source>
<target state="new">Multiple MSBuild project files found in '{0}'. Specify which to use with the --workspace option.</target>
......
......@@ -52,6 +52,11 @@
<target state="new">Loading workspace.</target>
<note />
</trans-unit>
<trans-unit id="Maximum_number_of_workspace_warnings_to_log_has_been_reached_Set_the_verbosity_option_to_the_diagnostic_level_to_see_all_warnings">
<source>Maximum number of workspace warnings to log has been reached. Set the --verbosity option to the 'diagnostic' level to see all warnings.</source>
<target state="new">Maximum number of workspace warnings to log has been reached. Set the --verbosity option to the 'diagnostic' level to see all warnings.</target>
<note />
</trans-unit>
<trans-unit id="Multiple_MSBuild_project_files_found_in_0_Specify_which_to_use_with_the_workspace_option">
<source>Multiple MSBuild project files found in '{0}'. Specify which to use with the --workspace option.</source>
<target state="new">Multiple MSBuild project files found in '{0}'. Specify which to use with the --workspace option.</target>
......
......@@ -52,6 +52,11 @@
<target state="new">Loading workspace.</target>
<note />
</trans-unit>
<trans-unit id="Maximum_number_of_workspace_warnings_to_log_has_been_reached_Set_the_verbosity_option_to_the_diagnostic_level_to_see_all_warnings">
<source>Maximum number of workspace warnings to log has been reached. Set the --verbosity option to the 'diagnostic' level to see all warnings.</source>
<target state="new">Maximum number of workspace warnings to log has been reached. Set the --verbosity option to the 'diagnostic' level to see all warnings.</target>
<note />
</trans-unit>
<trans-unit id="Multiple_MSBuild_project_files_found_in_0_Specify_which_to_use_with_the_workspace_option">
<source>Multiple MSBuild project files found in '{0}'. Specify which to use with the --workspace option.</source>
<target state="new">Multiple MSBuild project files found in '{0}'. Specify which to use with the --workspace option.</target>
......
......@@ -52,6 +52,11 @@
<target state="new">Loading workspace.</target>
<note />
</trans-unit>
<trans-unit id="Maximum_number_of_workspace_warnings_to_log_has_been_reached_Set_the_verbosity_option_to_the_diagnostic_level_to_see_all_warnings">
<source>Maximum number of workspace warnings to log has been reached. Set the --verbosity option to the 'diagnostic' level to see all warnings.</source>
<target state="new">Maximum number of workspace warnings to log has been reached. Set the --verbosity option to the 'diagnostic' level to see all warnings.</target>
<note />
</trans-unit>
<trans-unit id="Multiple_MSBuild_project_files_found_in_0_Specify_which_to_use_with_the_workspace_option">
<source>Multiple MSBuild project files found in '{0}'. Specify which to use with the --workspace option.</source>
<target state="new">Multiple MSBuild project files found in '{0}'. Specify which to use with the --workspace option.</target>
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册