Maintaining binary log compatibility

From dotnet/msbuild · View interactive version

Maintaining binary log compatibility is an AI agent skill from the GitHub repository dotnet/msbuild. Guides changes to MSBuild binary log infrastructure. Consult when modifying BinaryLogger or BinaryLogReplayEventSource, adding new BuildEventArgs types, changing event serialization/deserialization, modifying ProjectImportsCollector, adjusting message importance levels, or making changes that affect

How to use this skill

Skill file

Binary Log Considerations

The binary log (.binlog) is MSBuild's primary diagnostic format and source of truth for build analysis. It captures the complete build event stream with full fidelity. Format changes have strict compatibility requirements.

For general binary log usage, see Binary-Log.md.

Core Principles

  1. The binlog captures everything. All meaningful build events must be represented. If your change alters build behavior, it must be observable in the binlog.
  2. Format changes must be backward-compatible. Older versions of MSBuild Structured Log Viewer and other tools must be able to read binlogs produced by newer MSBuild, at minimum gracefully degrading.
  3. Forward compatibility matters too. Newer viewers should handle binlogs from older MSBuild without crashing, even if some events are unrecognized.

Architecture Overview

Build Engine
  → BuildEventArgs (structured events)
    → BinaryLogger (serializes to .binlog)
      → ProjectImportsCollector (embeds project/targets files)

Replay:
  .binlog file
    → BinaryLogReplayEventSource (deserializes)
      → Any ILogger (console, structured log viewer, analyzers)

Key source files:

Adding New Build Event Types

When adding a new BuildEventArgs subclass:

  1. Define the new event class inheriting from the appropriate base (BuildMessageEventArgs, BuildWarningEventArgs, etc.)
  2. Add serialization and forwarding support — implement WriteToStream and CreateFromStream, then trace the event through every dispatcher and transport (including task-host forwarding) without making unknown event types fatal
  3. Increment the binary log version if the new event type changes the format
  4. Add a new record type constant in the binary logger's record type enum
  5. Handle the unknown-type case in the replay source — older readers must skip gracefully

Serialization Compatibility Rules

// Pattern for backward-compatible field addition
if (logVersion >= newFieldVersion)
{
    writer.Write(newField);
}

// Reading with backward compatibility
if (logVersion >= newFieldVersion)
{
    newField = reader.ReadString();
}
else
{
    newField = defaultValue;
}

Message Importance Levels

Importance controls what appears in console output, but everything goes to binlog regardless of importance.

| Level | Use For | Console Verbosity |

|-------|---------|------------------|

| High | Critical user-facing information | Minimal and above |

| Normal | Standard build progress | Normal and above |

| Low | Detailed diagnostic information | Detailed and above |

| Diagnostic | Internal debugging | Diagnostic only |

Rules

ProjectImportsCollector

The ProjectImportsCollector embeds all imported .props, .targets, and project files into the binlog. This enables the "preprocessed view" in log viewers.

Considerations

Changes That Affect Binlog Content

When modifying MSBuild behavior, verify binlog impact:

| Change Type | Binlog Consideration |

|------------|---------------------|

| New property set during evaluation | Appears in PropertyInitialValue or PropertyReassignment events |

| New target added | Produces TargetStarted/TargetFinished events |

| Changed task behavior | Task output items/properties captured in TaskFinished |

| New warning/error | Captured as BuildWarningEventArgs/BuildErrorEventArgs |

| Modified import chain | Changes which files ProjectImportsCollector captures |

Testing Binlog Changes

Checklist

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.