Detecting Double-Writes in MSBuild using the binlog

  • Gérald Barré

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

  1. Open the console with MSBuild in the path:

    Open developer prompt for Visual StudioOpen developer prompt for Visual Studio

  2. Run msbuild with /bl flag to generate the binary log:

    msbuild solution.sln /bl

It outputs a file named msbuild.binlog:

Generated binlog fileGenerated binlog file

#Generating a binary log using Visual Studio

  1. Install the Visual Studio extension (VS2017 / VS2019): Project System Tools

  2. Install the Visual Studio extension (VS2022): Project System Tools

  3. Open the tool window under View > Other Windows > Build Logging

  4. Click the button Start logging builds

    Start logging builds in Visual StudioStart logging builds in Visual Studio

  5. Open the log

    Open binlogs created while building projects in Visual StudioOpen 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))
    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!

Follow me:
Enjoy this blog?Buy Me A Coffee💖 Sponsor on GitHub