NAnt scripting: emailing the build log - Ernst Kuschke

   Ernst Kuschke

     Arbitrary thoughts and musings on life, the universe and everything else

Syndication

News

    ernst kuschke (v1.0)

    My Photos

    Microsoft Most Valuable Professional

    Member in good standing

    View Ernst Kuschke's profile on LinkedIn

    Add to Technorati Favorites

Blogs I read

Books I recommend

General Links

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.

Published Tuesday, October 12, 2004 9:47 AM by Ernst Kuschke

Comments

# re: email notification from NANT@ Tuesday, October 12, 2004 9:49 AM

# re: NAnt scripting: emailing the build log@ Tuesday, October 12, 2004 11:07 AM

Much easier to use CruiseControl.Net to send your emails...

by KevinT

# re: NAnt scripting: emailing the build log@ Thursday, October 28, 2004 8:33 AM

Thanks! That was helpful and saved me a lot of time!

by D.C.

# re: NAnt scripting: emailing the build log@ Tuesday, February 22, 2005 4:24 AM

Hi Ernst:
I use the nant.onfailure property to specify a target that will get executed upon failure. In that specified target, I close the log file and then attach the log file to the mail. It works for me. I have demonstrated it <a href="http://www.instrumentalservices.com/index.php?option=com_content&task=view&id=40&Itemid=74">here</a>.

Thanks,
G

# re: NAnt scripting: emailing the build log@ Tuesday, March 15, 2005 10:18 PM

I have to say when I found this posting I jumped for joy!

by OvideM

# re: NAnt scripting: emailing the build log@ Wednesday, April 27, 2005 9:34 PM

I tried but I get <record name> is not recognized, what could be the reason for that?Also used it as following but doesn't work:

<property name="LOG_DIR" value="C:\DEVELOPMENT\Build_Scripts" />
<property name="CurrentBuild.Log" value="${LOG_DIR}/CurrentBuildLog.txt" />
<target name="CloseLogs" description="Closes log files - do not call

independently">
<record name="CurrentBuild.Log" action="Close" />
</target>


<target name="finish">

<copy file="CurrentBuild.log" tofile="AKbuildlog.log" />
<echo message="Sending email Status is ${build.datetime}"/>
****
</TARGET>

by AK

# re: NAnt scripting: emailing the build log@ Thursday, April 28, 2005 8:07 AM

Make sure you use forward slashes in your LOG_DIR property definition.

# re: NAnt scripting: emailing the build log@ Wednesday, April 16, 2008 7:41 AM

Hi,

Very useful info, just one query.

Is there way in the mail logger where we can send the log file as attachment instead of inline text.

Thanks

ss

by Satesh

Leave a Comment

(required) 
(required) 
(optional)
(required) 

Enter the numbers above: