Where is Task Scheduler console output? (C# console app)
Asked Answered
V

1

11

I am running a C# windows console application and passing in a couple of arguments via Task Scheduler. It runs all day loading flat file data created by other applications into SQL server. The program fails intermittently and I have Try/Catch logic that writes information regarding the exception using Console.WriteLine().

So basically I need to track down the location of the console output so I can take steps to eliminate the intermittent failures. I've had a good look around online thinking this must be a fairly common requirement to diagnose errors from Task Scheduler scheduled apps. My online search has revealed a couple of work around solutions but no direct answer as to where the console output goes when running a c# console application directly via Task Scheduler.

The two workarounds I have seen are:

(1) Get Task Scheduler to run a windows batch script (.bat file) instead of running the console app (.exe file) directly. Within the batch script use ">" or ">>" to redirect the console output to a flat file (e.g C:\app\myapp.exe arg1 arg2 > "C:\log\myapp_console_output.txt")

(2) A very similar solution is to get Task Scheduler to run the windows command line cmd.exe with /C option and passing arguments into cmd.exe to run my console app and also redirect the console output. e.g. something like: cmd.exe /C "C:\app\myapp.exe" arg1 arg2 > "C:\log\myapp_console_output.txt"

While I acknowledge that the above workarounds may well help me to capture future failures it doesnt really help me to track down output from the intermittent failures that have already occurred. They also seem quite messy workarounds to achieve what I would have thought was a pretty common standard requirement.

Can anyone please confirm that the console output is definitely not retained somewhere when running a c# console app directly via Task Scheduler?

Vicissitude answered 3/5, 2018 at 20:33 Comment(10)
Another option would be to have your console application write to a log file directly instead of the console, since the console output is not being monitored by anyone.Abiosis
I wouldn't think it's stored as the output could contain sensitive information. As @RufusL mentions, don't write to the Console if no one is going to read it. You could use a library such as Serilog to write both to a file and to the console thoughTravancore
Shouldn't the output/error log just appear in the Windows Event Viewer? But I agree with @RufusL and Camilo. You could just write the file out yourself.Avionics
Console.WriteLine does not show anywhere, when you run the exe as a scheduled task. You can write to the event log using Trace.Write()Frawley
Thanks guys. I had considered writing to a log file separately. It strikes me that there should be an option in Task Scheduler to capture/retain console output when required . There is an option in task scheduler to specify arguments so I just tried entering "argval1 argval2 > logfile.txt" the same as you would in command line, the redirect bit didnt work though.Vicissitude
Suggestion by @DodgerThud of looking at Windows event viewer was useful. It gave log of the errors and some diagnostic info, offsets etc, though not quite the detail that would have been captured by design in my code.Vicissitude
If you create a batch file (.bat) and enter the desired command with the "argval1 argval2 > logfile.txt" included, then you could use the Task Scheduler to run the batch script instead. I believe this would log the output like you were hoping for.Nomi
I think there are no better workarounds than that you mentioned unfortunately. And I don't agree with comments about logging to file. Despite its name, Console methods write to process standard output, and that's pretty normal thing to do. As evident from workarounds - someone starring at console is not a requirement to read this output.Enchorial
Thanks all. I dont write a lot of c# so some useful tips. Seems the way to go is to avoid using Console.WriteLine() and write to a log file directly as suggested by RufusL and Camilo Terevinto.The Trace.Write() suggested by jd_ is also interesting - I wont use that now but a useful idea for the future. Quite a few console apps start off being run on console and then moved onto a schedule later. Also a lot of the example code from microsoft also suggests using Console.WriteLine() to trap exceptions. I'll just avoid writing to the console in future.Vicissitude
Possible duplicate of How do I capture the output of a script if it is being ran by the task scheduler?Abiosis
D
-1

The technical answer is that task scheduler apps run on a separate desktop (session zero) you can’t easily access. There used to be a pop up that would give you access, but it’s not been available for a long time.

In Windows session 0, I was able to get the desktop even though there was no desktop, why? provides more details.

Doubleheader answered 14/6, 2023 at 2:10 Comment(2)
What does this have to do with where the stdout of the scheduled process went?Carlson
When a scheduled task runs, it creates a window in Session 0. For console apps, it creates a command parompt windows (or powershell if you're doing powershell) in session 0. Once whatever command / program you're using completes, that terminal window closes and any content in the window is lost.Doubleheader

© 2022 - 2024 — McMap. All rights reserved.