mark
mark

Reputation: 62712

How to output the duration of certain msbuild targets?

Today I copy/paste the following in each and every target I want to measure and log its duration:

  <Target Name="YabaDabaDoo" ...>
    <PropertyGroup>
      <Start>$([System.DateTime]::Now.Ticks)</Start>
    </PropertyGroup>
    ...
    <PropertyGroup>
      <End>$([System.DateTime]::Now.Ticks)</End>
      <Ticks>$([MSBuild]::Subtract($(End), $(Start)))</Ticks>
      <Elapsed>$([MSBuild]::Divide($(Ticks), 10000000))</Elapsed>
    </PropertyGroup>
    <Message Text="Duration: $(Elapsed) seconds" Importance="High"/>
  </Target>

I do not like it because it is too verbose and sometimes the target logic proper takes less space than duration logging.

Is there a way to implement it more concisely?

Upvotes: 0

Views: 316

Answers (1)

Kirill Osenkov
Kirill Osenkov

Reputation: 8976

Have you considered implementing a logger that would do that? See http://msbuildlog.com for an example.

Here's the documentation on how to implement your own logger: https://learn.microsoft.com/en-us/visualstudio/msbuild/build-loggers?view=vs-2017

If you run msbuild.exe /bl and then open the resulting .binlog in MSBuild Structured Log Viewer, it will show the durations of all targets in a timeline view:

Timeline view

Upvotes: 2

Related Questions