59

How do I output a variable value to the log from MSBuild?

I am trying to debug an MSBuild script and would like to output a variable's value to the log.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Simon
  • 33,714
  • 21
  • 133
  • 202
  • I think this question may help solve your problem. http://stackoverflow.com/questions/2968077/trouble-with-outputting-msbuild-variables – awright18 Jan 23 '11 at 02:22

1 Answers1

106

You can actually debug MSBuild scripts with Visual Studio 2010 now. It requires some hacking, and it isn't officially supported, but it is an option.

Otherwise use the Message task. Normal rules for referencing Properties, Items and Item Metadata (also referred to as batching) apply.

This example:

<Project DefaultTargets="Build"
         xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <ItemGroup>
    <TestItem Include="test1" />
    <TestItem Include="test2" />
    <TestItem Include="test3" />
  </ItemGroup>

  <PropertyGroup>
    <TestProperty>Property Value</TestProperty>
  </PropertyGroup>

  <Target Name="TestMessage" AfterTargets="Build" >

    <!-- Use $(Property Name) to reference a property -->
    <Message Text="$(TestProperty)" Importance="high"/>

    <!-- Use @(Item Name) to output a semi-colon
         separated list of items on one line      -->
    <Message Text="@(TestItem)" Importance="high"/>

    <!-- Use %(Item Name.Metadata Property Name) to 
         call the Message task once for each item.   -->
    <!-- This will output each item on a separate line -->
    <Message Text="%(TestItem.Identity)" Importance="high"/>

  </Target>
</Project>

Will produce this output:

Property Value
test1;test2;test3
test1
test2
test3
Kevin Panko
  • 8,356
  • 19
  • 50
  • 61
Aaron Carlson
  • 5,522
  • 4
  • 31
  • 35
  • 4
    To make your answer easier to use, consider adding Importance="high" attribute to each of the Message elements. This way the messages will be shown by default when you call msbuild or if you look in the build output in VS – Bogdan Gavril MSFT May 10 '15 at 08:49
  • 4
    Also, to hook this is quickly you may want to add the Target you just defined in an existing csproj and add a BeforeTargets="Build" to the target. Then just build with VS and look in the build output. – Bogdan Gavril MSFT May 10 '15 at 08:50
  • 3
    The output will show up where exactly? In Visual Studio's Output window (and "Show ouput from: Build")? Also, what file am I putting this in? I just tried appending this to the `C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\MSBuild\15.0\Bin\Microsoft.Common.CurrentVersion.targets` file, but I don't see anything in the output. – jbyrd Aug 17 '17 at 21:17
  • This is no longer valid: `Microsoft.Build.Exceptions.InvalidProjectFileException: The tag is no longer supported as a child of the element. Place this tag within a target, and add the name of the target to the "InitialTargets" attribute of the element.` – Vadim Peretokin Mar 29 '20 at 08:49
  • @VadimPeretokin, based on the error message you posted, it looks like you're putting the Message tag outside of a target. The error message is stating that it must be inside of a target. – Aaron Carlson Apr 01 '20 at 15:27
  • It's a csproj file that doesn't have any targets! How can I still print to the log? – Vadim Peretokin Apr 01 '20 at 17:23
  • @VadimPeretokin, A csproj does have targets, they just are not typically defined in the .csproj directly. They are imported from other files though [](https://learn.microsoft.com/en-us/visualstudio/msbuild/import-element-msbuild?view=vs-2019) elements. To help you gain a better understanding, you might want to [google](http://google.com/?q=how%20to%20execute%20a%20target%20before%20build%20msbuild) how to execute a target before build msbuild. – Aaron Carlson Apr 02 '20 at 20:38
  • But I don't need to execute any targets - I want to simply print a log statement. That's trivial to do in any programming language. Surely a simpler solution exists in MSBuild? – Vadim Peretokin Apr 03 '20 at 12:27
  • @VadimPeretokin, That is the only way I know how to log using msbuild. If you don't like that answer, you should probably google if you can log a message outside of a target. If you don't find an answer using google, feel free to ask a new question on stackoverflow. – Aaron Carlson Apr 03 '20 at 15:38