Windows任务调度程序 - 日志文件C#

时间:2014-04-29 13:51:04

标签: c# scheduled-tasks

我有一个包含app.config的c#程序。我有

<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

0 个答案:

没有答案