Double-writes are a common build problem that occurs when the same file is written more than once during a build. This typically happens when two different projects output the same files, causing unnecessary work that slows down the build and can break incremental builds.
MSBuild can detect files written multiple times during a build, letting you spot issues quickly, including in CI pipelines.
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:
Shell
msbuild 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.
C#
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 code above so it can be used in CI pipelines. The source code is available on GitHub. You can use the .NET tool Meziantou.MSBuild.Tools to analyze a binlog file:
Shell
dotnet tool update --global Meziantou.MSBuild.Tools
Shell
Meziantou.MSBuild.Tools detect-double-writes --path "msbuild.binlog"

Do you have a question or a suggestion about this post? Contact me!