Examples

To read issues from MsBuild log files you need to import the MsBuild issue provider needs to be imported:

build.cake
#addin nuget:?package=Cake.Issues&version=5.2.0
#addin nuget:?package=Cake.Issues.MsBuild&version=5.2.0

Note

In addition to the MsBuild issue provider the Cake.Issues core addin needs to be added.

Build.csproj
<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net9.0</TargetFramework>
    <RunWorkingDirectory>$(MSBuildProjectDirectory)</RunWorkingDirectory>
    <ImplicitUsings>enable</ImplicitUsings>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Cake.Frosting" Version="5.0.0" />
    <PackageReference Include="Cake.Frosting.Issues.MsBuild" Version="5.2.0" />
  </ItemGroup>
</Project>

The following example contains a task which will call MsBuild to build the solution and write a binary log file and a task to read issues from the binary log file and write the number of warnings to the console:

build.cake
var logPath = @"c:\build\msbuild.binlog";
var repoRootPath = MakeAbsolute(Directory("./"));

Task("Build-Solution").Does(() =>
{
    // Build solution.
    var msBuildSettings =
        new DotNetMSBuildSettings().WithLogger(
            "BinaryLogger," + Context.Tools.Resolve("Cake.Issues.MsBuild*/**/StructuredLogger.dll"),
            "",
            logPath.FullPath);
    DotNetBuild(
        repoRootPath.CombineWithFilePath("MySolution.sln").FullPath,
        new DotNetBuildSettings{MSBuildSettings = msBuildSettings});
});

Task("Read-Issues")
    .IsDependentOn("Build-Solution")
    .Does(() =>
    {
        // Read issues.
        var issues =
            ReadIssues(
                MsBuildIssuesFromFilePath(
                    logPath,
                    MsBuildBinaryLogFileFormat),
                repoRootPath);

        Information("{0} issues are found.", issues.Count());
});
Program.cs
using Cake.Common.Diagnostics;
using Cake.Common.IO;
using Cake.Common.Tools.DotNet;
using Cake.Common.Tools.DotNet.Build;
using Cake.Core;
using Cake.Core.IO;
using Cake.Frosting;

public static class Program
{
    public static int Main(string[] args)
    {
        return new CakeHost()
            .UseContext<BuildContext>()
            .Run(args);
    }
}

public class BuildContext(ICakeContext context) : FrostingContext(context)
{
    public FilePath LogPath { get; } = @"c:\build\msbuild.binlog";
    public DirectoryPath RepoRootPath { get; } =
        context.MakeAbsolute(context.Directory("./"));
}

[TaskName("Build-Solution")]
public sealed class BuildSolutionTask : FrostingTask<BuildContext>
{
    public override void Run(BuildContext context)
    {
        // Build solution.
        var msBuildSettings =
            new DotNetMSBuildSettings().WithLogger(
                "BinaryLogger," + context.Environment.ApplicationRoot.CombineWithFilePath("StructuredLogger.dll"),
                "",
                context.LogPath.FullPath);
        context.DotNetBuild(
            context.RepoRootPath.CombineWithFilePath("MySolution.sln").FullPath,
            new DotNetBuildSettings{MSBuildSettings = msBuildSettings});
    }
}

[TaskName("Read-Issues")]
[IsDependentOn(typeof(BuildSolutionTask))]
public sealed class ReadIssuesTask : FrostingTask<BuildContext>
{
    public override void Run(BuildContext context)
    {
        // Read issues.
        var issues =
            context.ReadIssues(
                context.MsBuildIssuesFromFilePath(
                    context.LogPath,
                    context.MsBuildBinaryLogFileFormat()),
                context.RepoRootPath);

        context.Information("{0} issues are found.", issues.Count());
    }
}

Tip

When using MSBuildSettings.BinaryLogger property to write a binary log, the version of the binary log format written depends on the version of the .NET SDK.

To avoid the risk of breaking builds when the .NET SDK is updated and introduces a new binary log format, which is not supported in the used version of Cake.Issues.MsBuild, the binary logger instance shipped as part of Cake.Issues.MsBuild is used in the above example.