NAnt scripting: emailing the build log
When creating our NAnt scripts one of the first issues I ran into was sending a copy of the buildlog to certain email addresses. What I innitially did was to create a NAnt target called sendmail that would attach the buildlog file and mail it to a list of interested parties. After building my assemblies, I would call this target, which looked like this:
<
target name="sendmail">
<mail
from=“NAnt@myCompany.com“
tolist=“${distribution_list}“
subject="${mailsubject}"
mailhost="${smtpServer}"
message="${mailbody}"
verbose="true"
>
<attachments>
<include name="${buildroot}/build.log" />
</attachments>
</mail>
</target>
This should work, right? Nope... The build.log file can't be attached to the email, since it is still being accessed by NAnt (it is still being written to). You will get an error that stipulates that access to the file was denied.
I then added a line that would make a copy of the build.log file before sending the email, and then attach this copy to the mail. The copy gets attached to the mail allright, but there is a new problem introduced: the log isn't complete. The buffer containing the build log hasn't completely been written to file at the time the mail is sent, with the result of only about half of the build.log being emailed out! I tried letting NAnt sleep before sending the email, but that also causes the writing of the log to sleep.
I finally found the solution, and it's simple. Forget about the sendmail target alltogether. NAnt has the MailLogger assembly which sends email out for you - you just need to set its properties, like so (set these right in the beginning of your script):
<
property name="MailLogger.mailhost" value="${smtpServer}" />
<property name="MailLogger.from" value="NAnt@myCompany.com" />
<property name="MailLogger.failure.notify" value="true" />
<property name="MailLogger.success.notify" value="true" />
<property name="MailLogger.failure.to" value="${failure_email_list}" />
<property name="MailLogger.success.to" value="${success_email_list}" />
To tell NAnt to use the MailLogger when logging, specify it on the commandline using the -logger parameter when running NAnt (I also specify a logfile here with the -l parameter):
nant -buildfile:my_buildfile.build -logger:NAnt.Core.MailLogger -l:my_build_log.log
This will send the log in an email as inline text.