From e9bca63cfd6a0530a5ffab3c5d71a6f986fb20ad Mon Sep 17 00:00:00 2001 From: Jonathon Marolf Date: Tue, 10 Mar 2020 10:06:52 -0700 Subject: [PATCH 1/2] WIP adding ability to produce a binlog --- src/CodeFormatter.cs | 14 +++++++++----- src/Program.cs | 3 ++- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/CodeFormatter.cs b/src/CodeFormatter.cs index c687fca9bc..6483d1f5cc 100644 --- a/src/CodeFormatter.cs +++ b/src/CodeFormatter.cs @@ -33,7 +33,8 @@ internal static class CodeFormatter public static async Task FormatWorkspaceAsync( FormatOptions options, ILogger logger, - CancellationToken cancellationToken) + CancellationToken cancellationToken, + bool createBinaryLog = false) { var (workspaceFilePath, workspaceType, logLevel, saveFormattedFiles, _, fileMatcher, reportPath) = options; var logWorkspaceWarnings = logLevel == LogLevel.Trace; @@ -45,7 +46,7 @@ internal static class CodeFormatter var workspaceStopwatch = Stopwatch.StartNew(); using (var workspace = await OpenWorkspaceAsync( - workspaceFilePath, workspaceType, fileMatcher, logWorkspaceWarnings, logger, cancellationToken).ConfigureAwait(false)) + workspaceFilePath, workspaceType, fileMatcher, logWorkspaceWarnings, logger, cancellationToken, createBinaryLog).ConfigureAwait(false)) { if (workspace is null) { @@ -147,7 +148,8 @@ private static string GetReportFilePath(string reportPath) Matcher fileMatcher, bool logWorkspaceWarnings, ILogger logger, - CancellationToken cancellationToken) + CancellationToken cancellationToken, + bool createBinaryLog = false) { if (workspaceType == WorkspaceType.Folder) { @@ -156,7 +158,7 @@ private static string GetReportFilePath(string reportPath) return folderWorkspace; } - return await OpenMSBuildWorkspaceAsync(workspacePath, workspaceType, logWorkspaceWarnings, logger, cancellationToken); + return await OpenMSBuildWorkspaceAsync(workspacePath, workspaceType, logWorkspaceWarnings, logger, cancellationToken, createBinaryLog); } private static async Task OpenMSBuildWorkspaceAsync( @@ -164,7 +166,8 @@ private static string GetReportFilePath(string reportPath) WorkspaceType workspaceType, bool logWorkspaceWarnings, ILogger logger, - CancellationToken cancellationToken) + CancellationToken cancellationToken, + bool createBinaryLog = false) { var properties = new Dictionary(StringComparer.Ordinal) { @@ -178,6 +181,7 @@ private static string GetReportFilePath(string reportPath) }; var workspace = MSBuildWorkspace.Create(properties); + workspace.ProduceBinaryLog = createBinaryLog; if (workspaceType == WorkspaceType.Solution) { diff --git a/src/Program.cs b/src/Program.cs index 037d45891d..5767a72ceb 100644 --- a/src/Program.cs +++ b/src/Program.cs @@ -150,7 +150,8 @@ public static async Task Run(string folder, string workspace, string verbos var formatResult = await CodeFormatter.FormatWorkspaceAsync( formatOptions, logger, - cancellationTokenSource.Token).ConfigureAwait(false); + cancellationTokenSource.Token, + createBinaryLog: logLevel == LogLevel.Trace ? true : false).ConfigureAwait(false); return GetExitCode(formatResult, check); } From e833443d67c82929d9da8fa94bd0d114f5f1cc6e Mon Sep 17 00:00:00 2001 From: Jonathon Marolf Date: Wed, 1 Apr 2020 07:14:25 -0700 Subject: [PATCH 2/2] consume new API --- src/CodeFormatter.cs | 16 +++++++++++++--- src/dotnet-format.csproj | 1 + 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/CodeFormatter.cs b/src/CodeFormatter.cs index 6483d1f5cc..96b93ec70b 100644 --- a/src/CodeFormatter.cs +++ b/src/CodeFormatter.cs @@ -9,6 +9,7 @@ using System.Text.Json; using System.Threading; using System.Threading.Tasks; +using Microsoft.Build.Logging; using Microsoft.CodeAnalysis.MSBuild; using Microsoft.CodeAnalysis.Options; using Microsoft.CodeAnalysis.Tools.Formatters; @@ -181,17 +182,26 @@ private static string GetReportFilePath(string reportPath) }; var workspace = MSBuildWorkspace.Create(properties); - workspace.ProduceBinaryLog = createBinaryLog; + + Build.Framework.ILogger binlog = null; + if (createBinaryLog) + { + binlog = new BinaryLogger() + { + Parameters = Path.Combine(Environment.CurrentDirectory, "formatDiagnosticLog.binlog"), + Verbosity = Build.Framework.LoggerVerbosity.Diagnostic, + }; + } if (workspaceType == WorkspaceType.Solution) { - await workspace.OpenSolutionAsync(solutionOrProjectPath, cancellationToken: cancellationToken).ConfigureAwait(false); + await workspace.OpenSolutionAsync(solutionOrProjectPath, msbuildLogger: binlog, cancellationToken: cancellationToken).ConfigureAwait(false); } else { try { - await workspace.OpenProjectAsync(solutionOrProjectPath, cancellationToken: cancellationToken).ConfigureAwait(false); + await workspace.OpenProjectAsync(solutionOrProjectPath, msbuildLogger: binlog, cancellationToken: cancellationToken).ConfigureAwait(false); } catch (InvalidOperationException) { diff --git a/src/dotnet-format.csproj b/src/dotnet-format.csproj index 86b4494a74..a61a130efb 100644 --- a/src/dotnet-format.csproj +++ b/src/dotnet-format.csproj @@ -25,6 +25,7 @@ +