C# Windows任务计划程序-日志文件C
我有一个c程序,它有app.config。我有C# Windows任务计划程序-日志文件C,c#,scheduled-tasks,C#,Scheduled Tasks,我有一个c程序,它有app.config。我有 <appSettings> <add key="logFile" value="Logs.txt"/> </appSettings> namespace LogTesting { class LogMessages { public static string WriteToFile(string Exceptionfrom, string text)
<appSettings>
<add key="logFile" value="Logs.txt"/>
</appSettings>
namespace LogTesting
{
class LogMessages
{
public static string WriteToFile(string Exceptionfrom, string text)
{
var dir = new System.IO.DirectoryInfo(Program.destnDirectoryName);
if (!dir.Exists) dir.Create();
var path = Path.Combine(dir.FullName, ConfigurationManager.AppSettings["logFile"]);
using (StreamWriter w = File.AppendText(ConfigurationManager.AppSettings["logFile"]))
{
Log(Exceptionfrom, text, w);
}
return path;
}
public static void Log(string Exceptionfrom, string logMessage, TextWriter w)
{
w.WriteLine("{0}\t{1} ", Exceptionfrom, logMessage);
}
}
}
using LogTesting;
namespace MainProgram
{
class Program
{
public const string destnDirectoryName = @"C:\tempLogs";
[STAThread]
static void Main(string[] args)
{
LogMessages.WriteFile("in main", DateTime.Now.ToString());
}
}
}
当我运行此F5或CTRL+F5时,日志将写入C:\tempLogs\Logs.txt。当我将其放入windows任务计划程序时,日志不会被写入。为什么
谢谢
R如果任务运行的凭据与运行Visual Studio实例的凭据不同,任务运行的凭据是什么?我建议您使用try/catch块,并查看在将其作为任务运行时是否出现任何错误。请检查提交作业的用户是否具有创建/写入该文件夹的权限。