为Windows开发计划任务

时间:2022-02-16 05:31:23

I have to develop an application using C#.net that has to be run once a day. It only runs for at most one minute, so developing a Windows service is overkill and a scheduled task is the appropriate way.

我必须使用C#.net开发一个必须每天运行一次的应用程序。它只运行最多一分钟,因此开发Windows服务是过度的,并且计划任务是适当的方式。

However, I have a few questions about how the application can communicate its results:

但是,我有一些关于应用程序如何传达其结果的问题:

  • How do I indicate to the task scheduler that the task has failed? Is this via the program's exit code?
  • 如何向任务计划程序指示任务已失败?这是通过程序的退出代码吗?

  • How do I log output information? Is console output automatically captured or do I have to write to the event viewer explicitly?
  • 如何记录输出信息?是自动捕获控制台输出还是我必须明确写入事件查看器?

4 个解决方案

#1


7  

In answer to your questions -

在回答你的问题 -

  1. If a task fails because it threw an unchecked exception you'll see that in the Sheduled Task viewer, there will be a 'Last Result' with a value something like 0xe0434f4d. Alternatively if you return an exit code that will be also be shown in the Last Result column of the Scheduled Task viewer.

    如果一个任务失败,因为它抛出了一个未经检查的异常,你会在Sheduled Task查看器中看到,会有一个“Last Result”,其值类似于0xe0434f4d。或者,如果您返回退出代码,该退出代码也将显示在“计划任务”查看器的“最后结果”列中。

  2. Writing to the console e.g. Console.WriteLine("blah"); won't show up anywhere. You'd need to write to the event log or to a log file.

    写入控制台,例如Console.WriteLine( “嗒嗒”);不会出现在任何地方。您需要写入事件日志或日志文件。

#2


4  

If you go the Log File way, you can still use Console.WriteLine("blah");. The trick is to redirect the standard Out and Error streams:

如果你采用日志文件的方式,你仍然可以使用Console.WriteLine(“blah”);.诀窍是重定向标准的Out和Error流:

        StreamWriter mylog = new StreamWriter("mylog.log");
        Console.SetOut(mylog);
        Console.SetError(mylog);

You also need to flush the buffers often, to make sure the log file contains current information.

您还需要经常刷新缓冲区,以确保日志文件包含当前信息。

            Console.Out.Flush();

This is quick and dirty, you really should use the Windows Event Log or log4net.

这很快,很脏,你真的应该使用Windows事件日志或log4net。

#3


0  

AFAIK the scheduler just kicks off a process. You can use the event log or another logging system to record the information you need to refer to later.

AFAIK调度程序只是启动一个进程。您可以使用事件日志或其他日志记录系统来记录稍后需要参考的信息。

#4


0  

Log4net is a very good, complete logging framework. I can highly recommend it.

Log4net是一个非常好的,完整的日志框架。我强烈推荐它。

#1


7  

In answer to your questions -

在回答你的问题 -

  1. If a task fails because it threw an unchecked exception you'll see that in the Sheduled Task viewer, there will be a 'Last Result' with a value something like 0xe0434f4d. Alternatively if you return an exit code that will be also be shown in the Last Result column of the Scheduled Task viewer.

    如果一个任务失败,因为它抛出了一个未经检查的异常,你会在Sheduled Task查看器中看到,会有一个“Last Result”,其值类似于0xe0434f4d。或者,如果您返回退出代码,该退出代码也将显示在“计划任务”查看器的“最后结果”列中。

  2. Writing to the console e.g. Console.WriteLine("blah"); won't show up anywhere. You'd need to write to the event log or to a log file.

    写入控制台,例如Console.WriteLine( “嗒嗒”);不会出现在任何地方。您需要写入事件日志或日志文件。

#2


4  

If you go the Log File way, you can still use Console.WriteLine("blah");. The trick is to redirect the standard Out and Error streams:

如果你采用日志文件的方式,你仍然可以使用Console.WriteLine(“blah”);.诀窍是重定向标准的Out和Error流:

        StreamWriter mylog = new StreamWriter("mylog.log");
        Console.SetOut(mylog);
        Console.SetError(mylog);

You also need to flush the buffers often, to make sure the log file contains current information.

您还需要经常刷新缓冲区,以确保日志文件包含当前信息。

            Console.Out.Flush();

This is quick and dirty, you really should use the Windows Event Log or log4net.

这很快,很脏,你真的应该使用Windows事件日志或log4net。

#3


0  

AFAIK the scheduler just kicks off a process. You can use the event log or another logging system to record the information you need to refer to later.

AFAIK调度程序只是启动一个进程。您可以使用事件日志或其他日志记录系统来记录稍后需要参考的信息。

#4


0  

Log4net is a very good, complete logging framework. I can highly recommend it.

Log4net是一个非常好的,完整的日志框架。我强烈推荐它。