How to Query historical task log with any conditions and and how to list instances of a task

Topics: Examples
Mar 9, 2015 at 5:36 AM
Edited Mar 9, 2015 at 5:39 AM
1. How to query historical task log with specified condition like “time create”, “correlation ID”..

Genertal code
public static void SchedulerHistory(string schedulerName)
{            
            using (TaskService ts = new TaskService())
            {
                Microsoft.Win32.TaskScheduler.Task task = ts.GetTask(schedulerName);
                TaskEventLog eventLogs = new TaskEventLog(task.Path);
                foreach (TaskEvent log in eventLogs)
                {                    
                    string level = log.Level;
                    DateTime? dateTime = log.TimeCreated;
                    int eventId = log.EventId;
                    string taskCategory = log.TaskCategory;
                    string code = log.OpCode;
                }
            }
}   
My code need to apply condition to query
1.I need to apply correlation ID as criteria to query.
2.I get correlationID from RunningTaskCollection of a task and use it to query history task later
Microsoft.Win32.TaskScheduler.Task task = GetTask(groupName);
// GetTask is my method that detail of this  have been written as you.
RunningTaskCollection runningTaskList = task.GetInstances();


    for (int i = 0; i < runningTaskList.Count; i++){

        Guid correlationID = runningTaskList[i].InstanceGuid;
 _      List<TaskEvent> list = (new TaskEventLog(path)).Where(lg => lg.ActivityId == correlationID).OrderBy(log => log.TimeCreated).Take(1).ToList();_
                foreach (TaskEvent log in list)
                {      

              // do something as my business logic              
                    string level = log.Level;
                    DateTime? dateTime = log.TimeCreated;
                    int eventId = log.EventId;
                    string taskCategory = log.TaskCategory;
                    string code = log.OpCode;
                }
}
My business logic is that I need to get first record of each transaction in history task with either correlationID or TimeCreated for doing something later as figure below.

As per my code, I take correlationID as condition, I am not sure whether this code is correct and efficient statement for getting task with LINQ .
It seem pretty slow because this statement has to load all records in history with “(new TaskEventLog(path))” and then using LINQ to query as conditin later.

Image


2. How to list all instances of each specified task name not only running instance but also finished instance (completed and error instance)
As far as I know, I can list only running instance from a task with below code.
Microsoft.Win32.TaskScheduler.Task task = GetTask(groupName);
// GetTask is my method that detail of this  have been written as you.
RunningTaskCollection runningTaskList = task.GetInstances();
However , I would like to get every instance of task both running and history.
Are there any ways to do this besides getting from historical task?.
Coordinator
Mar 11, 2015 at 4:55 PM
1) Look at the CorrelatedTaskEvent class in the TaskSchedulerEditor library. I believe it meets your requirements. If not, the code for that class should give you some ideas on how to pull the history from the EventLog using your code.
2) The GetInstances method only gets running instances. For historical instances, you will have to pull them from the EventLog.
Marked as answer by dahall on 9/1/2015 at 10:02 AM