Reputation: 65
Console.Write* works as expected when used with a console application, or a windowed application with a console attached.
However, if I make an application that does not have a console and run it from the command line, it does not output as expected. It appears to start the application asynchronously as the prompt reappears while the program is still running.
How can I output in such a way that text output will appear in an attached console or a command prompt that runs it? Or, how do I run the application differently where it waits correctly and captures the expected output?
Upvotes: 3
Views: 1265
Reputation: 156634
Consider using a Logger framework, rather than relying on Console. Logging frameworks provide a variety of ways to consume their logs, so you can decide which way works best for you without having to change your code.
For example, if you're using a logging framework that's set to output log messages to the console for the sake of a console app, you only have to change one configuration option to make it output to a file, which you can use a tool like Baretail to watch even if you don't have the console open. Or you can easily change the configuration to output to System.Diagnostics.Debug
so that it appears in the Visual Studio Output window.
Upvotes: 3
Reputation: 729
If you're just wanting debug output to appear in Visual Studio, use System.Diagnostics.Debug.WriteLine()
to output to the "Immediate" window in VS while the application is running. Wrap is in "IF" precompiler directives so that it won't happen when you compile into a non-debug build. Any lines inside the "IF" will be completely ignored by the compiler if the current build target does not have the "DEBUG" flag set (like the default "Release" build target).
#if DEBUG
System.Diagnostics.Debug.WriteLine()
#endif
Upvotes: 0