Skip to content

Commandline/object-model to inspect a binlog for errors/warnings #376

Closed

Description

@vatsan-madhavan

A typical way in with file logs are used esp. in local/dev-builds goes like this:

$warn = Get-Content msbuild.wrn
$err = Get-Content msbuild.err 

if (warn.length -gt 0) {
    Write-Error "Warnings Found"
}

if (err.length -gt 0) {
    Write-Error "Errors Found"
}

I'm wondering if there is a simple way to replace file-logs with binary-logs for this type of scenario. Specifically, if there is a simple approach (for e.g., using powershell) to inspect a binary log for these (without replaying the log using msbuild - that would defeat the purpose of replacing file-logs with binary-logs):

  • Number of errors (or whether errors exist)
    • The error text
  • Number of warnings (or whether warnings exist)
    • The warning text

Activity

  1. KirillOsenkov commented on Jul 9, 2020

    @KirillOsenkov
    Owner

    Yes, everything the log viewer can do is also available via an API. All you need is StructuredLogger.dll from this NuGet package: https://www.nuget.org/packages/MSBuild.Structuredlogger

    Here's an example of reading start/end times and durations of targets:
    https://github.com/KirillOsenkov/MSBuildStructuredLog/blob/master/src/Samples/TimesAndDurations/Program.cs

    You probably want your build to restore the NuGet package, and then write a simple csx file (C# interactive, that you can run via csi.exe), or PowerShell, or a simple console app that reads the binlog, reads all warnings, errors and prints them to the console, potentially using colors or tables.

    More examples are available at:
    https://msbuildlog.com/#api

  2. vatsan-madhavan commented on Jul 11, 2020

    @vatsan-madhavan
    Author

    I'll have to experiment with it to see how to read errors, warnings etc. I'll reach out again if I encounter problems.

  3. KirillOsenkov commented on Jul 12, 2020

    @KirillOsenkov
    Owner

    Yes, try build.FindChildrenRecursive<Error> and build.FindChildrenRecursive<Warning>

  4. KirillOsenkov commented on Jul 12, 2020

    @KirillOsenkov
    Owner

    you can bucketize them by parent project, sort, filter, etc

  5. vatsan-madhavan commented on Jul 12, 2020

    @vatsan-madhavan
    Author

    This is what I ended up figuring out, which seems to work as expected.

    Serialization.Read(fileName)
                .FindChildrenRecursive<Warning>()
                .Select(w => (w.Code, w.Text, w.Timestamp));

    The only surprise is the time it takes to read a log through Serialization.Read.

    When dealing with text logs, testing its size or length is a quick operation. In this case, a simple program that reads and searches a 30MB binary-log for warnings takes about 50 seconds or so to load using Serialization.Read.

    Somehow the viewer is able to load the log much faster.

    What can I do to read and query logs faster?

  6. vatsan-madhavan commented on Jul 12, 2020

    @vatsan-madhavan
    Author

    Something else I just noticed - .NET Framework is way faster that .NET Core (.NET Core is trimmed, ready-to-run, win-x64)

    C:\source\repos\MSBuildBinLogSummarizer\MSBuildBinLogSummarizer> Measure-Command { .\bin\Release\net472\publish\MSBuildBinLogSummarizer.exe --log-file D:\src\repos\pinvoke\msbuild.binlog}
    
    Days              : 0
    Hours             : 0
    Minutes           : 0
    Seconds           : 10
    Milliseconds      : 601
    Ticks             : 106019084
    TotalDays         : 0.000122707273148148
    TotalHours        : 0.00294497455555556
    TotalMinutes      : 0.176698473333333
    TotalSeconds      : 10.6019084
    TotalMilliseconds : 10601.9084
    
    
    C:\source\repos\MSBuildBinLogSummarizer\MSBuildBinLogSummarizer> Measure-Command { .\bin\Release\netcoreapp3.1\win-x64\MSBuildBinLogSummarizer.exe --log-file D:\src\repos\pinvoke\msbuild.binlog}
    
    Days              : 0
    Hours             : 0
    Minutes           : 0
    Seconds           : 54
    Milliseconds      : 150
    Ticks             : 541507554
    TotalDays         : 0.000626744854166667
    TotalHours        : 0.0150418765
    TotalMinutes      : 0.90251259
    TotalSeconds      : 54.1507554
    TotalMilliseconds : 54150.7554
    
    
  7. KirillOsenkov commented on Jul 12, 2020

    @KirillOsenkov
    Owner

    The viewer sets the serverGC mode in app.config, which results in less GC pauses. There’s also a lower level API ReadRecords() that reads the raw records and doesn’t build the tree, it should be faster. Try that? There’s a sample on the website.

  8. vatsan-madhavan commented on Jul 13, 2020

    @vatsan-madhavan
    Author

    Even with server GC and ReadRecords, I'm not doing much better (around 40 secs on netcoreapp3.1, 4 seconds on net472). I'm calling into Replay directly since that and ReadRecordsFromDecompressedStream (which ReadRecords seems to use) seem to have similar complexity anyway, and Replay lets me just do one pass over the binary log to summarize stats.

    Here is what I'm doing - StructuredLoggerUtil.cs

    Here is my binlog (personal OneDrive link to zipped file) - https://1drv.ms/u/s!Ah63gfGkKca4lsl2sy71gpkpM9TCog?e=ifgsMj. If you'd rather generate it yourself locally, it's just a binlog generated from https://github.com/vatsan-madhavan/pinvoke/tree/enable-binlog by running build.ps1 -Restore -Build -BinaryLog

  9. KirillOsenkov commented on Jul 13, 2020

    @KirillOsenkov
    Owner

    Hey @stephentoub @danmosemsft Vatsan has found an interesting scenario where reading a binlog takes 4 seconds on .NET Desktop, but 40 seconds on Core 3.1.

    Repro:

    1. git clone https://github.com/vatsan-madhavan/MSBuildStructuredLogSummary
    2. msbuild /r
    3. Download the binlog from https://1drv.ms/u/s!Ah63gfGkKca4lsl2sy71gpkpM9TCog?e=ifgsMj and save it anywhere, e.g. C:\temp\vatsan\vatsan.binlog
    4. MSBuildBinLogSummary\bin\Debug\net472\MSBuildBinLogSummary.exe --log-file C:\temp\Vatsan\vatsan.binlog - takes 4 seconds
    5. MSBuildBinLogSummary\bin\Debug\netcoreapp3.1\MSBuildBinLogSummary.exe --log-file C:\temp\Vatsan\vatsan.binlog - takes 40 seconds

    The stack that I saw was taking all the time is ReadByte from a GzipStream, it's called millions of times (to read the binlog):

      | ZLibNative.ZLibStreamHandle.Inflate Line 300
    -- | --
      | Inflater.Inflate Line 281
      | Inflater.ReadInflateOutput Line 266
      | Inflater.ReadOutput Line 122
      | Inflater.InflateVerified Line 92
      | Inflater.Inflate Line 55
      | DeflateStream.ReadByte Line 229
      | GZipStream.ReadByte Line 72
      | BinaryReader.ReadBoolean Line 236
      | BuildEventArgsReader.ReadTargetFinishedEventArgs Line 359
      | BuildEventArgsReader.Read Line 71
      | BinLogReader.Replay Line 78
      | BinLogReader.Replay Line 37
      | StructuredLoggerUtil..ctor Line 30
      | Program.ProcessBinaryLog Line 29
      | Program.Main Line 24
    

    Is this something known? On Desktop Framework I see a similar native inflate stack, but it doesn't cost nearly as much. Would someone be able to take a look? Thanks!

  10. danmoseley commented on Jul 13, 2020

    @danmoseley

    Not something I know of. cc @carlossanlop who own this area. Could you paste this repro into a new issue in dotnet/runtime for us?

  11. KirillOsenkov commented on Jul 13, 2020

    @KirillOsenkov
    Owner

    Reported here, thanks!
    dotnet/runtime#39233

  12. KirillOsenkov commented on Dec 15, 2020

    @KirillOsenkov
    Owner

    Fixed the perf issue with this commit: dba8091

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions