Log messages in task

Problem

You want to write task messages to a persistent log file.

Solution

Get the reference to the ILog interface by calling GetProperty and passing in TaskPropertiesLogger.

C#
ILog logger = this.GetProperty<ILog>(TaskProperties.Logger);

With an instance of the ILog interface, all log methods are written to the standard host log files by default.

C#
logger.Error("My text");
logger.Fatal("should appear");
logger.Fatal("in the file");
C#
using System;
using AGI.Parallel.Client;
using AGI.Parallel.Infrastructure;
using AGI.Parallel.Infrastructure.Logging;

namespace CodeSamples
{
    class Program
    {
        static void Main(string[] args)
        {
            using (IJobScheduler scheduler = new ClusterJobScheduler("localhost"))
            {
                scheduler.Connect();
                Job job = scheduler.CreateJob();
                job.AddTask(new WriteSomeToLog());
                job.Submit();
                job.WaitUntilDone();
            }

            /*
             * Go to %ALLUSERSPROFILE%\AGI\STK Scalability 1.3\logs to inspect the log files.
             */
        }

        [Serializable]
        class WriteSomeToLog : Task
        {
            public override void Execute()
            {
                ILog logger = this.GetProperty<ILog>(TaskProperties.Logger);

                logger.Error("My text");
                logger.Fatal("should appear");
                logger.Fatal("in the file");
            }
        }
    }
}
Discussion

Logging can be important for debugging, tracing, and post-mortem analysis. The ILog interface provides "free" logging within tasks. The main advantage to using the ILog interface versus a custom logging mechanism is that ILog is tightly integrated with the existing logging infrastructure. For instance, right clicking on a task within the monitoring applications displays the user log messages in the same logs as the host.

See Also

STK Scalability 1.3 API for .NET