dahall / TaskScheduler

Provides a .NET wrapper for the Windows Task Scheduler. It aggregates the multiple versions, provides an editor and allows for localization.
MIT License
1.21k stars 191 forks source link

TaskEventWatcher create wrong filter #850

Closed novakj23 closed 4 years ago

novakj23 commented 4 years ago

I try the example for the watcher. I run the scheduler task during sleep but it does n`t trigger the handler.

var task = ts.FindTask("Firefox Default Browser Agent 308046B0AF4A39CB"); var watcher = new TaskEventWatcher(task); watcher.BeginInit(); watcher.Filter.EventIds = new int[] { 102, 201, 110, 200, 100 }; watcher.Folder = task.Folder.Path; ; watcher.EventRecorded += Watcher_EventRecorded; watcher.IncludeSubfolders = true; watcher.SynchronizingObject = null; watcher.Enabled = true; watcher.EndInit(); Thread.Sleep(100000);

I would like to use the library so I spend some time debugging the situation. The problem is converting DateTime to XML. Exactly in this line

"System.Xml.XmlConvert.ToString(startTime.Value, System.Xml.XmlDateTimeSerializationMode.RoundtripKind)".

StartTime.Value has the time: image

DateTime has specific field Kind on value Local. Microsoft documentation for enum "System.Xml.XmlDateTimeSerializationMode.RoundtripKind" says that "Time zone information should be preserved when converting.".

Result of converting is:

TimeCreated[@SystemTime>='2020-06-20T16:51:51.5770282+02:00']}

The time is not some. It is shifted for 2 hours. I used the .Net Framework 4.5.2

For the example of using TaskEventLog is DateTime in the constructor. But the TaskEventWatcher doesn`t have a parameter for setting StartDate. It is used DateTime.Now for the initializing an object of TaskEventLog. The better way is to use the UTC timezone.

I try to change the code to DateTime.UtcNow : image

The DateTime has value: image

The date is converted to correct format:

TimeCreated[@SystemTime>='2020-06-20T14:49:06.7642685Z']}

I recommend changing the parameter in the constructor of TaskEventLog inside the TaskEventWatcher and use DateTime.UtcNow .

dahall commented 4 years ago

@novakj23 Thank you for doing all the work to debug this. I'll do some testing and then release. Prior to that, you can find the latest packages on the NuGet feed at https://ci.appveyor.com/nuget/taskscheduler-prerelease.