如何在 msbuild 任务中读取日志文件并检查每一行



我想读取在构建过程中创建的日志文件并检查它是否包含一些错误消息。我想打印出带有错误消息的行。

到目前为止,我可以读取文件并再次打印整个文件,但我无法检查几行异常文本并仅打印这些行。有人给我提示吗?

<Target Name="CheckLog">
<PropertyGroup>
<ErrorText>ERR</ErrorText>
</PropertyGroup>
<ReadLinesFromFile File="execution.log" >
<Output TaskParameter="Lines" ItemName="FileContents" />
</ReadLinesFromFile>
<!-- lines are printed out correctly -->
<Message Text="%(FileContents.Identity)" />
<CreateProperty Value="true" Condition="@(FileContents.Contains('$(ErrorText)'))">
<Output TaskParameter="Value" PropertyName="VerifyError" /> 
</CreateProperty>
<Message Text="@(VerifyError)" />
<Error Condition="@(VerifyError) != ''" Text="Execution returned with an error." />
</Target>

我自己找到了解决方案。

首先,我向消息任务添加了一个条件。所以这些行被过滤了。仅打印出指示错误的行。

其次,我替换了 CreateProperty 任务。我现在使用 PropertyGroup 创建一个包含所有行的属性(不完全是我最初想要的,但它与下一点一起对我有用(。

第三,我修改了错误条件。我检查全文中是否发生了错误。

<Target Name="CheckLog">
<PropertyGroup>
<ErrorText>ERR</ErrorText>
</PropertyGroup>
<ReadLinesFromFile File="execution.log" >
<Output TaskParameter="Lines" ItemName="FileContents" />
</ReadLinesFromFile>
<Message Text="%(FileContents.Identity)" Condition="$([System.String]::Copy('%(FileContents.Identity)').StartsWith($(ErrorText)))" />
<PropertyGroup>
<AllLines>@(FileContents)</AllLines>
</PropertyGroup>
<Error Condition="$(AllLines.Contains($(ErrorText)))" Text="Execution returned with an error." />
</Target>

也许更优雅: 仅使用错误任务:

<Target Name="CheckLog">
<PropertyGroup>
<ErrorText>ERR</ErrorText>
</PropertyGroup>
<ReadLinesFromFile File="execution.log" >
<Output TaskParameter="Lines" ItemName="FileContents" />
</ReadLinesFromFile>
<Error Text="%(FileContents.Identity)" Condition="$([System.String]::Copy('%(FileContents.Identity)').StartsWith($(ErrorText)))" />
</Target>

最新更新