Language: C#
View on GitHub to download or comment.
See the User script help topic for additional information.
This script allows you to include warning and error messages logged in the Job Log when sending a notification message for the job. For example, if you have a Response to send a notification when the job fails, you can use this script to include more detailed information from the Job Log.
This script uses the adTempus API to get information about the executing job instance. It reads the Job Log messages for the instance to find all warning and error messages. It combines these messages into a single string, which it stores in the "ErrorMessagesForInstance" Job Variable. This variable can be used to insert the messages where you want them to appear.
Note: The user account the job is running under must have an adTempus login with permission to read the job. This is necessary so the API can read the job details.
Create a new Shared Script named "Copy error and warning messages to variable". Set the language to C# and paste the sample code as the script body. Click OK to save the script.
In your job, edit the Response that you use to send the notification message. Add a new Action to "Execute a script". In the Script to execute section select the "Copy error and warning messages to variable" script. Move this Action up so that it is run before your notification action.
In your notification message, insert the variable token %ErrorMessagesForInstance% where you want the messages to appear. This token will be replaced at execution with the list of messages.
By default this script includes both warning and error messages. If you only want to include errors, modify the code to set IncludeWarningMessages=false.
/*
This script reads the Job Log for the executing instance and finds all Error and (optionally) Warning messages that have been logged. It combines these into a
string and stores them in the ErrorMessagesForInstance Job Variable so they can be used in a notification message.
The user account that the job is running under must have a login in adTempus, with permission to read the job.
By default the script includes Warning messages. To include only errors, set IncludeWarningMessages=false below.
*/
using System;
using System.Text;
using System.Linq;
using System.Collections.Generic;
using ArcanaDevelopment.adTempus.ApplicationIntegration;
using ArcanaDevelopment.adTempus.Client;
using ArcanaDevelopment.adTempus.Shared;
namespace UserScript
{
public class UserScript : ArcanaDevelopment.adTempus.ApplicationIntegration.UserScriptBase
{
//Set to true to include warning messages, or false to include error messages only
private static readonly bool IncludeWarningMessages=true;
public override Object Run()
{
//get the identity of the running instance from the JobVariable. Note that this won't be set if you test the script outside of a running job, so this will fail.
var instanceOID=new OID(ClassID.ExecutionHistoryItem,Guid.Parse(adTempus.JobVariables["ADJobInstance"]));
var messageString=new StringBuilder();
//connect to adTempus using the API. Note that the use account used for the job must have permission to log in to adTempus and view the job
using(var scheduler=Scheduler.Connect(adTempus.JobVariables["ServerAndInstanceName"],LoginAuthenticationType.Windows, "", ""))
{
using(var context=scheduler.NewDataContext())
{
//Get the ExecutionHistoryItem representing the active instance (documentation.arcanadev.com/adtempus/api/5.1/ArcanaDevelopment.adTempus.Client~ArcanaDevelopment.adTempus.Client.ExecutionHistoryItem.htm)
var instance=(ExecutionHistoryItem)context.GetObject(instanceOID);
var messages=instance.LogMessages.OrderBy(msg => msg.RecordID); //sort in ascending order. Don't use timestamp because messages may all end up with the same timestamp if they're logged in quick succession.
foreach(var message in messages)
{
if(message.MessageType == MessageType.Error || (IncludeWarningMessages && message.MessageType== MessageType.Warning))
{
if(message.MessageType==MessageType.Error)
{
messageString.Append("Error: ");
}
else if (message.MessageType == MessageType.Warning)
{
messageString.Append("Warning: ");
}
messageString.AppendLine(message.Message);
}
}
//store the messages in a variable
adTempus.JobVariables.Add("ErrorMessagesForInstance", messageString.ToString(), false);
}
}
return 0;
}
}
}
View on GitHub to comment.