diff --git a/src/CodeFormatter.cs b/src/CodeFormatter.cs index 03e5bc9182..858b7e9a94 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) { @@ -150,7 +152,8 @@ private static string GetReportFilePath(string reportPath) Matcher fileMatcher, bool logWorkspaceWarnings, ILogger logger, - CancellationToken cancellationToken) + CancellationToken cancellationToken, + bool createBinaryLog = false) { if (workspaceType == WorkspaceType.Folder) { @@ -159,7 +162,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( @@ -167,7 +170,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) { @@ -182,15 +186,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 bb7879d644..89da7a48fa 100644 --- a/src/Program.cs +++ b/src/Program.cs @@ -154,7 +154,8 @@ public static async Task Run(string? folder, string? workspace, string? ver 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 2d9e875f16..d950cb65f6 100644 --- a/src/dotnet-format.csproj +++ b/src/dotnet-format.csproj @@ -26,6 +26,7 @@ +