diff --git a/src/CodeFormatter.cs b/src/CodeFormatter.cs index c687fca9bc..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; @@ -33,7 +34,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 +47,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 +149,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 +159,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 +167,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) { @@ -179,15 +183,25 @@ private static string GetReportFilePath(string reportPath) var workspace = MSBuildWorkspace.Create(properties); + 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/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); } 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 @@ +