StreamWriter does not write to file when called from a Scheduler task C #

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.

+4
2

, - C:\Windows\System32, . .

, , . , , - , . Environment.GetFolderPath SpecialFolder (, ApplicationData.)

, File.AppendAllText , .

+10

Jon Skeet , , SpecialFolder , ,
catch, catch { } , catch - .

, - , :

catch
{
    // WHO CARES
}

, - -, . .

+2

Source: https://habr.com/ru/post/1523336/


All Articles