I have the following function that takes a string and writes the contents of the string to a log file.
private static void LogEvent(string sEvent)
{
sEvent = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss") + "|" + sEvent;
Console.WriteLine(sEvent);
try
{
using (StreamWriter oStreamWriter = new System.IO.StreamWriter("MyService_" + DateTime.Now.ToString("yyyyMMdd") + ".log", true))
{
oStreamWriter.WriteLine(sEvent);
}
}
catch
{
}
}
When the program that calls the function is launched manually from the command line or by double-clicking on the executable file, a log file is created or added.
The problem is that I set this program to be called from the task scheduler every 10 minutes, and for some reason the code runs correctly, except that the program does not create or add a log file.
A scheduled task calls the program using the same user rights as when manually starting the program.
Why is this happening, and how can I fix it.