Problem

You want to send an email to a System Administrator when an error occurs.

Solution

Configure the AGI.Parallel.Host.exe.config or AGI.Parallel.Host_x86.exe.config file to set up the required log4net xml section. For the cluster scheduler, the default location is C:\Program Files (x86)\AGI\STK Scalability 1.2\Agent\bin\AGI.Parallel.Host.exe.config. The file will need write permissions. Add the appender XML element as described in the example code. Once that is done, an email will be sent every time a task logs an error message.

Tip

This same pattern can be used to send emails when an error occurs on the agent and the coordinator.

C#  Copy imageCopy
using System;
using AGI.Parallel.Client;
using AGI.Parallel.Infrastructure;
using AGI.Parallel.Infrastructure.Logging;

namespace CodeSamples
{
    class Program
    {
        static void Main(string[] args)
        {
            /*
             * First, edit your AGI.Parallel.Host.exe.config to add a SMTP appender.
             * This is an example of the log4net xml element.
             * Other examples can be found at http://logging.apache.org/log4net/release/config-examples.html
             * 
<log4net>
  <root>
    <priority value="Debug"/>
    <appender-ref ref="SmtpAppender"/>
  </root>
  <appender name="SmtpAppender" type="log4net.Appender.SmtpAppender">
    <to value="to@domain.com" />
    <from value="from@domain.com" />
    <subject value="test logging message" />
    <smtpHost value="SMTPServer.domain.com" />
    <bufferSize value="512" />
    <lossy value="true" />
    <evaluator type="log4net.Core.LevelEvaluator">
        <threshold value="ERROR"/>
    </evaluator>
    <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%newline%date [%thread] %-5level %logger [%property{NDC}] - %message%newline%newline%newline" />
    </layout>
  </appender>
</log4net>
             */

            using (IJobScheduler scheduler = new ClusterJobScheduler("localhost"))
            {
                scheduler.Connect();
                Job job = scheduler.CreateJob();
                job.AddTask(new TaskThatWritesToLoggingError());
                job.Submit();
                job.WaitUntilDone();
            }
        }

        [Serializable]
        public class TaskThatWritesToLoggingError : Task
        {
            public override void Execute()
            {
                ILog log = this.GetProperty<ILog>(TaskProperties.Logger);
                log.Error("This could be sent to your email");
            }
        }
    }
}

Discussion

Logging and email can be important for being notified when a task fails or a certain event happens in the system. Internally, log4net is used for logging. Thus, all of the features and properties that log4net provides are available for use.

Use log4net's SmptpAppender to email a user when a task fails. For more examples of different log4net logging examples, see this page: http://logging.apache.org/log4net/release/config-examples.html.