0

I have a problem with a third-party COM library. I'm using C# to call a function from the library. It works, but it unexpectedly writes some debug data to the console window. I tried redirecting the output to a file, but it didn't succeed. I tried to redirect output streams from 1 to 5.

The commands I use for redirection look like

myapp.exe 1> output.txt

How is it possible to write to the console, but not to the output stream?

I've also tried:

myapp.exe 1> output.txt 2> outputerr.txt 3> output3.txt 4> output4.txt 5> output5.txt

All files were created. File output.txt had my own strings I'd printed with Console.WriteLine. File output3.txt captured Windows command prompt after the program finished. Other files were empty. The data were still printed to screen.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
AlexVB
  • 217
  • 4
  • 11
  • That's only redirecting stdout... if you use `myapp.exe 1> output.txt 2> error.txt` then it should do both stdout and stderr. (You say you've tried redirecting streams 1-5, but you haven't shown how you've tried anything other than 1... and if you only redirect one stream, then if it writes to both stdout and stderr, you'll never get both.) – Jon Skeet Jan 13 '22 at 09:11
  • As an aside, another option is to use `Console.SetOut` and `Console.SetError` from within your code. I don't know for sure whether that will help with a COM library though. – Jon Skeet Jan 13 '22 at 09:12
  • @JonSkeet Thank you for the idea. I tried SetOut and SetError, but didn't get any new behaviour. – AlexVB Jan 13 '22 at 09:47
  • Could it be related to [TraceListener's](https://stackoverflow.com/questions/1159755/where-does-system-diagnostics-debug-write-output-appear)? Just guessing. – Peter Mortensen Jan 19 '22 at 01:03
  • Isn't it possible to identify the stream number somehow? – Peter Mortensen Jan 19 '22 at 01:11
  • In PowerShell, [stream 5 is for Write-Debug](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_output_streams?view=powershell-7.2). – Peter Mortensen Jan 19 '22 at 01:12
  • *[Writing C# debug output to .txt file](https://stackoverflow.com/questions/7926577/)* – Peter Mortensen Jan 19 '22 at 01:15
  • Wild guesses. Let us know about any progress... – Peter Mortensen Jan 19 '22 at 01:15

1 Answers1

0

Please check also whether you have in your Visual Studio Application Properties --> Application --> Output Type = "Console Application". If you have set to "Windows Application" you cannot redirect from command line to a file like: YourApp.exe > log.txt

winny2
  • 1
  • 1