我怎样才能查看当前的“步骤”?通过 MSBuild 中的自定义记录器对项目列表执行任务?

发布于 2024-10-03 11:00:27 字数 495 浏览 0 评论 0原文

我提前道歉,因为我什至不知道如何正确表达我需要的内容,所以我将尝试更详细地解释这一点:

我们正在使用 MSBuild 构建一个自动化部署流程。我们的软件安装程序需要在我们环境中的某些计算机上运行多次,因为我们必须设置单独的实例。我们这样做的方法是创建要完成的任务的“列表”(如果这是正确的术语,则为 @(name) 之类的变量),并使用元数据描述要运行的安装、运行它的实例以及运行的内容机器,然后循环遍历所有这些。此外,我们正在以同步方式关闭和重新启动服务。

本质上,我们的问题在于想要分离出故障所在,并将其限制在特定实例中以供记录器使用。当发出警告时,有什么方法可以将当前任务的属性暴露给记录器?理想情况下(为了让它有任何用处),它将是后解析的,所以如果该属性是:

<Exec Command="install%(InstanceName.ShortName).bat" />

我们可以在记录器中查看该属性并查看当前它正在运行“installfoo.bat”吗?

I apologize in advance for not even knowing how to correctly phrase what I need, so I'll try and explain this in more detail:

We are building an automated deployment process using MSBuild. Our software installer needs to be run multiple times on some machines in our environment as we have to set up separate instances. The way we're doing this is by creating 'lists' (if that's the correct term, variables like @(name)) of tasks to be completed with metadata describing the install to run, for which instance to run it, and on what machine, and then cycling through all of those. In addition, we're shutting down and restarting services in a synchronized way.

Essentially, our problem lies in wanting to split out where failures are and restricting them to a specific instance for the purposes of our logger. Is there any way that the current task's properties are exposed to the logger when warnings are raised? Ideally (for this to be of any use) it would be post-parsing, so if the property is:

<Exec Command="install%(InstanceName.ShortName).bat" />

Can we look into that property in a logger and see that currently it's running 'installfoo.bat'?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

多情出卖 2024-10-10 11:00:27

您所描述的功能称为批处理。如果我正确地阅读了您的问题,您希望能够记录发生错误或其他事件时构建所在的批次。

看看ILogger接口,我认为这是不可能的——只有任务知道哪一批项目已经传递给它。我猜您想对一堆不同的内置任务执行此操作,因此一种解决方法是将任务包装在一个目标中,您可以在其中记录您喜欢的任何内容。

<Project xmlns=...>
    <Target Name="Build">
        <ItemGroup>
            <InstanceName Include="Foo Instance">
                <ShortName>foo</ShortName>
            </InstanceName>
            <InstanceName Include="Bar Instance">
                <ShortName>bar</ShortName>
            </InstanceName>
        </ItemGroup>

        <MSBuild Projects="$(MSBuildProjectFile)"
                 Targets="BatchedExec"
                 Properties="CurrentInstance=%(InstanceName.Identity);
                     CurrentShortName=%(ShortName)">
        </MSBuild>
    </Target>

    <Target Name="BatchedExec">
        <Message Text="CurrentInstance: $(CurrentInstance)" />
        <Message Text="CurrentShortName: $(CurrentShortName)" />

        <Exec Command="install$(CurrentShortName).bat">
            <Output TaskParameter="ExitCode" PropertyName="ExitCode"/>
        </Exec>

        <Message Text="install$(CurrentShortName).bat exited with code $(ExitCode)." />
    </Target>
</Project>

在此示例中,为每个批次调用 BatchedExec 目标,您可以在调用任务之前记录您喜欢的任何信息。您可以为要使用批处理参数运行的每个任务制定类似的目标。

The feature you're describing is called batching. If I'm reading your question correctly, you want to be able to log what batch the build is on when an error, or some other event, occurs.

Looking at the ILogger interface I don't think it can be done - only the task knows what batch of items has been passed to it. I'm guessing you want to do this for a bunch of different built-in tasks, so one workaround would be to wrap the task in a target where you can log whatever you like.

<Project xmlns=...>
    <Target Name="Build">
        <ItemGroup>
            <InstanceName Include="Foo Instance">
                <ShortName>foo</ShortName>
            </InstanceName>
            <InstanceName Include="Bar Instance">
                <ShortName>bar</ShortName>
            </InstanceName>
        </ItemGroup>

        <MSBuild Projects="$(MSBuildProjectFile)"
                 Targets="BatchedExec"
                 Properties="CurrentInstance=%(InstanceName.Identity);
                     CurrentShortName=%(ShortName)">
        </MSBuild>
    </Target>

    <Target Name="BatchedExec">
        <Message Text="CurrentInstance: $(CurrentInstance)" />
        <Message Text="CurrentShortName: $(CurrentShortName)" />

        <Exec Command="install$(CurrentShortName).bat">
            <Output TaskParameter="ExitCode" PropertyName="ExitCode"/>
        </Exec>

        <Message Text="install$(CurrentShortName).bat exited with code $(ExitCode)." />
    </Target>
</Project>

In this example, the BatchedExec target is called for each batch, and you can log whatever information you like before you call the task. You'd make similar targets for each task you wanted to run with batched arguments.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文