开发者

How can I look into the current 'step' of a task being performed on an list of items via a custom logger in MSBuild?

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 corre开发者_运维技巧ct 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'?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜