如何从MSBuild向日志输出变量值

时间:2021-07-12 03:12:35

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

如何从MSBuild向日志输出变量值?

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

我正在尝试调试MSBuild脚本,并希望将变量值输出到日志中。

1 个解决方案

#1


56  

You can actually debug msbuild scripts with VS2010 now. It requires some hacking and it isn't officially supported but it is an option.

您现在可以使用VS2010实际调试msbuild脚本。它需要一些黑客攻击,它不受官方支持,但它是一种选择。

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

否则使用Message任务。适用于引用属性,项和项元数据(也称为批处理)的常规规则。

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

#1


56  

You can actually debug msbuild scripts with VS2010 now. It requires some hacking and it isn't officially supported but it is an option.

您现在可以使用VS2010实际调试msbuild脚本。它需要一些黑客攻击,它不受官方支持,但它是一种选择。

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

否则使用Message任务。适用于引用属性,项和项元数据(也称为批处理)的常规规则。

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