【问题标题】:How to output a variable value to the log from MSBuild如何从 MSBuild 将变量值输出到日志
【发布时间】:2011-01-23 02:18:21
【问题描述】:

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

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

【问题讨论】:

标签: .net debugging msbuild


【解决方案1】:

您现在实际上可以使用 Visual Studio 2010 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

【讨论】:

  • 为了使您的答案更易于使用,请考虑将 Importance="high" 属性添加到每个 Message 元素。这样,当您调用 msbuild 或在 VS 中查看构建输出时,默认情况下会显示消息
  • 另外,要快速挂钩,您可能需要添加刚刚在现有 csproj 中定义的目标,并将 BeforeTargets="Build" 添加到目标。然后只需使用 VS 构建并查看构建输出。
  • 输出会显示在哪里?在 Visual Studio 的“输出”窗口(和“显示来自:构建”的输出)?另外,我把这个放在什么文件里?我刚刚尝试将其附加到 C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\MSBuild\15.0\Bin\Microsoft.Common.CurrentVersion.targets 文件中,但我在输出中看不到任何内容。
  • 这不再有效:Microsoft.Build.Exceptions.InvalidProjectFileException: The &lt;Message&gt; tag is no longer supported as a child of the &lt;Project&gt; element. Place this tag within a target, and add the name of the target to the "InitialTargets" attribute of the &lt;Project&gt; element.
  • @VadimPeretokin,根据您发布的错误消息,您似乎将 Message 标签放在目标之外。错误消息指出它必须在目标内。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-07-28
  • 1970-01-01
  • 2018-10-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多