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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4771913/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 15:10:04  来源:igfitidea点击:

How to output a variable value to the log from MSBuild

.netdebuggingmsbuild

提问by Simon

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

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

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

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

回答by Aaron Carlson

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

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

Otherwise use the Messagetask. Normal rules for referencing Properties, Itemsand Item Metadata(also referred to as batching) apply.

否则使用Message任务。用于引用一般规则PropertiesItemsItem Metadata(也被称为配料)申请。

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