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

时间:2011-01-23 02:18:21

标签: .net debugging msbuild

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

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

1 个答案:

答案 0 :(得分:63)

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

否则使用Message任务。引用PropertiesItemsItem Metadata(也称为batching)的常规规则适用。

这个例子:

<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>

将产生此输出:

Property Value
test1;test2;test3
test1
test2
test3