Detecting Double-Writes in MSBuild using the binlog
Double-writes are a common problem when building an application. This occurs when the same file is written at least twice during a build. This could be because 2 different projects are outputting the same files, so you are doing unneeded work. Thus, the build is slower than it needs to be. Also, it may break some incremental builds.
MSBuild can detect files written multiple times during compilation, so you can spot issues quickly. You can even detect double-writes in your CI builds.
First, you need to generate a binary log file.
#Generating a binary log using the command line
Open the console with MSBuild in the path:
Open developer prompt for Visual Studio
Run
msbuild
with/bl
flag to generate the binary log:Shellmsbuild solution.sln /bl
It outputs a file named msbuild.binlog
:
Generated binlog file
#Generating a binary log using Visual Studio
Install the Visual Studio extension (VS2017 / VS2019): Project System Tools
Install the Visual Studio extension (VS2022): Project System Tools
Open the tool window under
View > Other Windows > Build Logging
Click the button
Start logging builds
Start logging builds in Visual Studio
Open the log
Open binlogs created while building projects in Visual Studio
#Finding double-writes using the GUI
Install MSBuild Binary and Structured Log Viewer, and then open the file in the tool. If the build has double-writes, the tool will show the following message:
#Finding double-writes programmatically
You can use the MSBuild.StructuredLogger
NuGet package to open and analyze a binlog file.
var binlogPath = "sample.binlog";
var build = Serialization.Read(binlogPath);
foreach (var doubleWrite in DoubleWritesAnalyzer.GetDoubleWrites(build))
{
doubleWritesCount++;
Console.WriteLine($"Multiple writes to {doubleWrite.Key}");
foreach (var source in doubleWrite.Value)
{
Console.WriteLine("- " + source);
}
}
#Finding double-writes using a CLI
I've written a CLI using the previous code, so I can use it in CI pipelines. The code source is available on GitHub. You can use the .NET tool Meziantou.MSBuild.Tools to analyze a binlog file:
dotnet tool update --global Meziantou.MSBuild.Tools
Meziantou.MSBuild.Tools detect-double-writes --path "msbuild.binlog"
Do you have a question or a suggestion about this post? Contact me!