Get host log that executes a task

If you only need to view the log of the hosts that executed your tasks, the monitoring applications should be sufficient. But it may still be useful in some cases to programatically return the host log back to the client. Use cases include providing your application some visual text for the end user when an error occurs. You can programatically get the log of the host by calling GetHostLog. The GetHostLog method is available on the IGetHostLog interface.

C#
using System;
using System.Threading;
using AGI.Parallel.Client;
using AGI.Parallel.Infrastructure;
using AGI.Parallel.Infrastructure.Logging;

namespace CodeSamples
{
    class Program
    {
        static void Main(string[] args)
        {
            TaskState ourTaskState = null;

            using (IJobScheduler scheduler = new ClusterJobScheduler("localhost"))
            {
                scheduler.Connect();

                Job job = scheduler.CreateJob();
                job.AddTask(new WriteSomeToLog());

                // You need to store the TaskState that you get from TaskSTateChanged
                job.TaskStateChanged += (notUsed, taskState) =>
                    {
                        if (taskState.State.ProcessId > 0)
                        {
                            ourTaskState = taskState.State;
                        }
                    };

                job.Submit();
                job.WaitUntilDone();

                HostRequestLogResultMessage logResult = null;
                ManualResetEvent logIsReady = new ManualResetEvent(false);

                // You have to check if your IJobScheduler interface supports IGetHostLog
                IGetHostLog canGetHostLog = scheduler as IGetHostLog;

                // Use the TaskState object you got above to call GetHostLog
                canGetHostLog.GetHostLog(
                    ourTaskState.AssignedWorker.Id,
                    TaskExecutionType.DotNet,
                    ourTaskState.ProcessId,
                    (hostLog) =>
                    {
                        logResult = hostLog;
                        logIsReady.Set();
                    });

                logIsReady.WaitOne();

                string contents = string.Join(Environment.NewLine, logResult.Messages);


                Console.WriteLine("Log Contents:");
                Console.WriteLine(contents);
            }

            /*
             * The output of the application should resemble:
             * Log Contents:
             * DEBUG 2013-02-25 10:51:17,554 - host received internal configuration: {} 
             * INFO  2013-02-25 10:51:20,795 - setting up Task Environment ...
             * DEBUG 2013-02-25 10:51:20,815 - adding assembly dependency SimpleExample, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
             * INFO  2013-02-25 10:51:20,890 - executing task #1 (SimpleTask)...
             * DEBUG 2013-02-25 10:51:21,004 - resolved assembly "SimpleExample, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null" using task environment
             * DEBUG 2013-02-25 10:51:21,671 - resolved assembly "AGI.Parallel.Infrastructure, Version=0.0.0.0, Culture=neutral, PublicKeyToken=46f7a65aaf1b26a0" using already loaded assembly
             * DEBUG 2013-02-25 10:51:22,039 - starting cancel timer, effective cancel time is 30000ms
             * INFO  2013-02-25 10:51:22,041 - tearing down Task Environment...
             */
        }

        [Serializable]
        internal 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");
            }
        }
    }
}
See Also

STK Parallel Computing Server 2.9 API for .NET