Reputation: 3970
So I have an EXEC task in MSBuild that runs a utility. This utility writes a log file, but does not produce output on stdout.
In the case of an error (the utility returns a ReturnCode of something other than 0), I would like to grab the contents of the log file and output a message based on the contents.
The problem is the log file is large, and I'm only interested in a small part of it. And I can't figure out how to extract that small part for inclusion in the build log error notification. Yet it's important for people to understand WHAT went wrong, not just that something went wrong.
The file format is something like this, in the case of an error:
Line1
Line2
Line3
*** Something Bad Happened
Details on something bad
*** Run Failed
LineX
LineY
LineZ
So what I need is a way to just extract the lines, starting from the first line that starts with three asterisks, continuing to the next line that starts with three asterisks, INCLUSIVE. I don't need to worry about the case where there are more than two lines that start with three asterisks, as that never happens.
Is this even remotely possible? I cannot figure out the magic incantation if so. I'd be willing to settle (in the short term) for just grabbing the first line with three asterisks, and being able just log THAT, but I'd really like to get the entire block of lines.
Upvotes: 1
Views: 366
Reputation: 10432
<Target Name="LogError" Condition="$(ExitCode) != 0">
<PropertyGroup>
<ErrorFile>foo.log</ErrorFile>
<ErrorRead>$([System.IO.File]::ReadAllText($(ErrorFile)))</ErrorRead>
<ErrorText>$([System.Text.RegularExpressions.Regex]::Match($(ErrorRead), '^\*\*\* [\s\S]*^\*\*\* .*', System.Text.RegularExpressions.RegexOptions.Multiline))</ErrorText>
</PropertyGroup>
<Error File="$(ErrorFile)" Text="$(ErrorText)" />
</Target>
Upvotes: 1