I'm trying to run a simple console application in windows task scheduler and to log some information which appear in the task "History" tab.
I figured that the content of "History" tab comes from the following windows event log : Microsoft-Windows-TaskScheduler/Operational, but i can't managed to write anything in it. I think I should then managed to link the event log entry to the task instance id, but that's another problem.
static void Main(string[] args)
{
FileStream fs = new FileStream(@"C:\Temp\log.txt", FileMode.Truncate, FileAccess.Write);
using (StreamWriter sw = new StreamWriter(fs))
{
sw.WriteLine(DateTime.Now.ToLocalTime() + " : Main start");
using (EventLog eventLog = new EventLog("Microsoft-Windows-TaskScheduler/Operational"))
{
eventLog.Source = "TaskScheduler";
try
{
eventLog.WriteEntry("Log message example", EventLogEntryType.Warning, 101, 1);
}
catch (Exception e)
{
sw.WriteLine(DateTime.Now.ToLocalTime() + " : error : " + e.ToString());
}
}
sw.WriteLine(DateTime.Now.ToLocalTime() + " : Main end");
}
}
I don't have any exeption in my "secondary" log file called og.txt. Following is the content of the log.txt file after one task execution :
21/03/2016 19:48:07 : Main start
21/03/2016 19:48:07 : Main end
At the moment my task execute with 'System' account with maximal authorization, but i also tried with my own account (i'm admin) with maximal authorization.