Quantcast
Channel: Task Scheduler Managed Wrapper
Viewing all articles
Browse latest Browse all 2206

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

$
0
0
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?.

Viewing all articles
Browse latest Browse all 2206

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>