【问题标题】:Windows Task Scheduler EXE log file not generatingWindows 任务计划程序 EXE 日志文件未生成
【发布时间】:2022-12-14 14:28:00
【问题描述】:

我已经安排了一个 EXE,我在其中编写了一个代码来将所有日志保存在一个文件中。当我在本地运行该项目时,会生成日志,但是当在任务计划程序中计划 exe 时,不会生成日志文件。我检查了我的调试文件夹路径,但文件没有在那里生成。我也检查了这条路径:C:\\Windows\System32\ScheduleProcessing_Errors。但它也不是在那里生成的。

这就是我写日志的方式:

public static void WriteErrorLog(string MsgBody, string innerexp, string Caller, string MethodName)
        {
            try
            {
                string Path;
                string Todaydate = System.DateTime.Now.ToString("dd-MMM-yyyy");
                Path = Application.StartupPath + "\\ScheduleProcessing_Errors";
                if (Directory.Exists(Path) == false)
                {
                    Directory.CreateDirectory(Path);
                }
                Path += "\\NewLogToFindMyErr" + Todaydate + ".txt";
                StreamWriter SW = new StreamWriter(Path, true);
                SW.WriteLine(DateTime.Now);                
                SW.WriteLine(Caller + "\t" + MethodName + "\t" + MsgBody + "\t" + innerexp);
                SW.WriteLine(Environment.NewLine);
                SW.WriteLine("-------------------------------------------");
                
                SW.Write(Environment.NewLine);
                SW.Close();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.message);
            }
        }

【问题讨论】:

    标签: c# windows-task-scheduler


    【解决方案1】:

    在设置任务计划程序时,无论用户是否登录都选择运行,并选择不存储密码。它将允许访问计算机本地资源

    【讨论】: