Tim Calladene
Tim Calladene

Reputation: 126

How to see MsBuild output in Visual Studio in real time

I've added some additional targets to a .csproj file in order to carry out some additional tasks after the project build is completed.

Nothing appears in the Visual Studio output window until all targets have completed. I want to be able to see messages that occur as the targets are being processed.

If I use the MSBuild Task Explorer (a VS extension), I can see that the messages can be picked up by a Visual Studio window as they are generated, so am I just missing a setting somewhere?

I've also tried replacing the Exec tasks with SmartExec from the MSBuild Extensions package.

Here is a snippet from my .csproj project file:

<Target Name="PostBuildActions" AfterTargets="Build">
<!--Get the version number from the assembly info -->
<GetAssemblyIdentity AssemblyFiles="$(ProjectDir)$(OutputPath)$(TargetFileName)">
  <Output TaskParameter="Assemblies" ItemName="ToolboxVersion" />
</GetAssemblyIdentity>
<CreateProperty Value="$(ProjectDir)$(OutputPath.TrimEnd('\'))">
  <Output TaskParameter="Value" PropertyName="ToolboxTarget" />
</CreateProperty>
<!-- Run the Simulink Widget Generator tool -->
<CreateProperty Value="&quot;$(SolutionDir)SimulinkWidgetGenerator\bin\$(Configuration)\SimulinkWidgetGenerator.exe&quot; -v %(ToolboxVersion.Version) -d &quot;$(ToolboxTarget)&quot;">
  <Output TaskParameter="Value" PropertyName="WidgetGenCommand" />
</CreateProperty>
<Message Text="Running Simulink Widget Generator:" Importance="High" />
<Message Text="$(WidgetGenCommand)" Importance="High" />
<Exec Command="$(WidgetGenCommand)" ConsoleToMSBuild="true" />
<!-- Invoke Matlab -->
<CreateProperty Value="try, PackageToolbox, catch ex, disp(getReport(ex)), exit(-1), end, exit(0);">
  <Output TaskParameter="Value" PropertyName="MatlabScript" />
</CreateProperty>
<CreateProperty Value="&quot;$(MATLAB_INSTALL_DIR)\bin\matlab.exe&quot; -automation -wait -log -sd &quot;$(ToolboxTarget)&quot; -r &quot;$(MatlabScript)&quot;">
  <Output TaskParameter="Value" PropertyName="MatlabCommand" />
</CreateProperty>
<Message Text="Invoking Matlab: " Importance="High" />
<Message Text="$(MatlabCommand)" Importance="High" />
<Exec Command="$(MatlabCommand)" ConsoleToMSBuild="true" />

Upvotes: 2

Views: 3223

Answers (3)

gojimmypi
gojimmypi

Reputation: 536

The key to seeing MSBuild output in realtime is to use the MSBuild project SDK. Many thanks to rainersigwald that posted this solution on GitHub:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
    <RootNamespace>_5451</RootNamespace>
  </PropertyGroup>

  <Target Name="LogStuffInRealTime" BeforeTargets="CoreCompile">
    <Exec Command="ping 127.0.0.1" YieldDuringToolExecution="True" ConsoleToMSBuild="true" StandardOutputImportance="high">
      <Output TaskParameter="ConsoleOutput" ItemName="OutputOfExec" />
    </Exec> 
  </Target>
</Project>

Upvotes: 1

LoLance
LoLance

Reputation: 28196

For this issue, I recommend you use msbuild command like msbuild xxx.csproj by developer command prompt to see the targets being processed.

enter image description here

So am I just missing a setting somewhere?

No, indeed you're right and for now, the output in Visual studio seems to not support for real-time display after my test.

Details to describe this situation:

As we know, there has two ways to build vs project: 1. Build in Visual Studio 2. Msbuild.exe.

Build process in VS(#1) actually calls the Msbuild tool(#2) to work.

Msbuild tool will display the target to a console window in real-time.

And in VS, the output of its build seems like a Non-real-time log, which will display after the build process ends.If we add a Time-consuming operation like yours, it won't display until the command ends. I've done a test for this, create a simple test.csproj, and add a script like this:

 <Target Name="WaitingToDoSth" AfterTargets="Test1">
    <Exec Command="$(ProjectDir)DoSth.exe"/>
  </Target>

This DoSth.exe has a Thread.sleep(3000) in it. In VS, the output won't display anything until the DoSth.exe executes successfully and the entire build process ends. When using msbuild xxx.csproj command in developer command prompt for VS2017, the display can be real-time and we can see messages that occur as the targets are being processed.

If my answer is helpful, please give a feedback. Thank you.

Upvotes: 1

baruchiro
baruchiro

Reputation: 5841

In Visual Studio, you can config your MSBuild verbosity in Tools –> Options –> Projects and Solutions –> Build and Run.

MSBuild project build output verbosity

From here:

Verbosity set to Quiet – shows either success or the build failure. 1 line displayed below for successful build.

Verbosity set to Minimal – shows the command line for the build. 2 lines displayed for successful rebuild.

Verbosity set to Normal. Shows the output from the MSBuild Targets. 25 lines displayed for successful rebuild.

Verbosity set to Detailed. Much more comments shown from MSBuild. 395 lines displayed for successful build.

And lastly, Verbosity set to Diagnostic, shows you everything. 1097 lines displayed for successful build.

Upvotes: 1

Related Questions