Email Errors in MSBuild - ReadLinesFromFile “is be

2019-07-09 16:43发布

问题:

I'm trying to implement the answer from here: How to access error details in MSBuild

> msbuild MyProject.proj /fl /flp:v=detailed;logfile=mylog.txt

<Target Name="ErrorEmail">
   <ReadLinesFromFile
      File="mylog.txt"
      Lines="_ErrorLines"
      />
   <Mail
      SmtpServer="mysrv"
      From="me@mysrv"
      To="error@mysrv"
      Subject="An error occured"
      Body="Error details: @(_ErrorLines, '%0D%0A')"
      />
</Target>

Looks elegant, but I'm getting this error:

c:\AccuRev\Build_2012_01_02\MyApp\ErrorHandlers.targets(24,9): error MSB3501: Could not read lines from file "mylog.txt". The process cannot access the file 'c:\AccuRev\Build_2012_01_02\MyApp\mylog.txt' because it is being used by another process

回答1:

mylog.txt file is locked because you are using the same file for build log and for the ReadLinesFromFile task.

EDIT: Try to execute MSBuild 2 times. First to build your projects and 2nd time to send an email. Use distributedFileLogger command line switch with /flp1:logfile=errors.txt;errorsonly command line parameters to log all errors to errors.txt. You can then attach this file into your email without sending whole build log.



回答2:

I know it's late for the party, but I was struggling with this myself: How to access the error log in order to attach it to an email? Was getting the same message--the error log file couldn't be accessed to attach it, or even to read it.

The solution? Copy the log file and then send the copy. Worked like a charm. My code looks something like this:

<Target Name="FailBuild">
  <Copy SourceFiles="errors.txt" DestinationFiles="errors_email.txt" />
  <Mail SmtpServer="$(SmtpEmailServer)"
      ...
      Attachments="errors_email.txt" />
</Target>