This post was most recently updated on December 27th, 2018.
Every now and then, you run into a situation, where you’ll need to somehow dump the console output (or transcript) of running a console application. I’m actually going to argue it happens a lot more often than one would think – in my case, any time a customer requires a webjob or a function, that one would normally deploy to Azure, being ran on the servers of the customer. This post describes, how to do that.
Something breaks or the app crashes, and the error is logged to event log… But just the error, not the whole transcript. You’d like to get it all, to figure out what’s actually going on, but event log is not the way to go.
Or, you’re investigating an error that happened to someone else, but only get screenshots of console or event log errors, whereas you’d want to get all the possible information about the problem instead.
What to do?
Solution: redirect the console output directly to a file
It’s luckily pretty easy. There are multiple ways to pipe, dump, mirror or just save the output, but since I hate always googling for them (and for the life of me, I can’t seem to remember it by heart), I’m documenting my preferred way here.
You can direct the whole console output (and hence the whole PowerShell transcript for your executable) to a text file by doing something like this:
executable.exe > output.txt 2>&1
executable.exe *>&1 > output.txt
This method just writes everything from the console window to a file, as simple as that!
In these examples:
|Redirection operator: >||Writes the command output to a file or a device, such as a printer, instead of the Command Prompt window.|
|2>&1||So this command first redirects stdout to the output file, and then redirects stderr there as well.|
Okay – now it’s documented. Finally!
Maybe I won’t have to google it the next time :)
Wait… But how is this better than copypasting from a console window?
If you’re not running your application unattended. you could just run the application and copy-paste from the window to your preferred location. That’s nice and easy! So what makes this method better?
A couple of things come to mind:
- You can get the transcripts unattended. You don’t have to do anything yourself.
- This way, You won’t mess the copying up by selecting from areas of the output.
- I don’t know about you, but I often mess up the copy-pasting from a console or PowerShell window. This method makes that impossible.
- This is the easiest way I’ve found to ask other people for the whole output/transcript of a console application run.
- That’s really useful, because when I’m debugging, I really want the whole log, and not just the last few lines of red text!
For more info and options about the output scenarios, see this Stack Overflow thread.
However, a far more advanced scenario would be to save the output directly to Application Insights or something similar. For a lot of cases, this would be like shooting a fly with a bazooka, but for larger deployments, why not. Maybe worth a blog post later!
Latest posts by Antti K. Koskela (see all)
- Fixing “-2147024891, System.UnauthorizedAccessException” when accessing SharePoint SOAP Web Services - January 17, 2019
- “500 Internal Server Error” after switching a WordPress site to PHP 7.3 - January 15, 2019
- Azure Functions failing on “OPTIONS” call? Quick fix! - January 9, 2019
- Thanks for an amazing 2018! - December 28, 2018