Console StdOut ans StdErr redirection is only partial. Why and how to redirect full Console output to memory (stream, TextWriter, ...) ?

Eric Ouellet 41 Reputation points
2022-11-01T20:37:55.42+00:00

The problem occur in a WPF application. Console StdOut and StdErr redirection does not works for C++ library, it only works for C# code.

This is code used to redirect in my WPF app:

Outputter = new StreamWriter(@"C:\temp\test.txt");  
Console.SetOut(Outputter);  
Console.SetError(Outputter);  

This code does not show all output data sent to the Console from a C++ dll. Why and how to ensure that everything would be redirected, not only in C# but also in a DLL writen in C++?

Note: If I use this code: accepted answer, then everything is redirected properly in a file, either output from C++ dll. The problem is we don't want a file. We want to redirect directly in a string without a file.

Note: I cannot include the C++ code which is a commercial product.

Sample of output with redirection (WPF app). The ouput is partial compared to console app:

Started
Adding variables ...
Adding constraints ...
Adding objectives ...
Model built, starting optimization ...
x[1,1] = 1

Sample Output when the application is a console application :

Started  
Adding variables ...  
Adding constraints ...  
Adding objectives ...  
Model built, starting optimization ...  
x[1,1] = 1  
n64)  
Thread count: 10 physical cores, 20 logical processors, using up to 20 threads  
Optimize a model with 178554 rows, 106557 columns and 827442 nonzeros  
Model fingerprint: 0xa9efbb41  
Variable types: 54717 continuous, 51840 integer (51840 binary)  
Coefficient statistics:  
  Matrix range     [7e-04, 4e+02]  
  Objective range  [1e-05, 1e+02]  
  Bounds range     [1e+00, 1e+00]  
  RHS range        [1e+00, 3e+03]  
Presolve removed 88855 rows and 45450 columns  
Presolve time: 3.02s  
Presolved: 89699 rows, 61107 columns, 731972 nonzeros  
Variable types: 35340 continuous, 25767 integer (25767 binary)  
  
Deterministic concurrent LP optimizer: primal and dual simplex  
Showing first log only...  
  
  
Root simplex log...  
  
Iteration    Objective       Primal Inf.    Dual Inf.      Time  
   47199    1.7808421e+04   1.021279e+03   1.857243e+09      5s  
Concurrent spin time: 0.00s  
  
Solved with dual simplex  
  
Root relaxation: objective 9.260857e+03, 43881 iterations, 4.73 seconds (6.89 work units)  
  
    Nodes    |    Current Node    |     Objective Bounds      |     Work  
 Expl Unexpl |  Obj  Depth IntInf | Incumbent    BestBd   Gap | It/Node Time  
  
     0     0 9260.85746    0  174          - 9260.85746      -     -    8s  
     0     0 10094.3736    0  436          - 10094.3736      -     -   10s  
     0     0 10094.3778    0  426          - 10094.3778      -     -   11s  
     0     0 10094.3903    0  507          - 10094.3903      -     -   12s  
     0     0 10094.3905    0  508          - 10094.3905      -     -   12s  
     0     0 10094.4241    0  514          - 10094.4241      -     -   14s  
H    0     0                    10195.371156 10094.4241  0.99%     -   14s  
     0     0 10094.4257    0  469 10195.3712 10094.4257  0.99%     -   15s  
     0     0 10094.4298    0  502 10195.3712 10094.4298  0.99%     -   16s  
H    0     0                    10095.583734 10094.4298  0.01%     -   17s  
     0     0 10094.4306    0  510 10095.5837 10094.4306  0.01%     -   17s  
     0     0 10094.4314    0  465 10095.5837 10094.4314  0.01%     -   18s  
     0     0 10094.4314    0   78 10095.5837 10094.4314  0.01%     -   19s  
     0     0 10094.5635    0  271 10095.5837 10094.5635  0.01%     -   19s  
H    0     0                    10095.583277 10094.5635  0.01%     -   19s  
     0     0 10094.5752    0  266 10095.5833 10094.5752  0.01%     -   19s  
H    0     0                    10095.491716 10094.5752  0.01%     -   19s  
     0     0 10094.6001    0  274 10095.4917 10094.6001  0.01%     -   20s  
H    0     0                    10095.396619 10094.6001  0.01%     -   20s  
H    0     0                    10095.176923 10094.6001  0.01%     -   20s  
     0     0 10094.6015    0  278 10095.1769 10094.6015  0.01%     -   20s  
     0     0 10094.6773    0  296 10095.1769 10094.6773  0.00%     -   20s  
     0     0 10094.6773    0  322 10095.1769 10094.6773  0.00%     -   20s  
H    0     0                    10094.999649 10094.6773  0.00%     -   20s  
H    0     0                    10094.999344 10094.6773  0.00%     -   20s  
     0     0 10094.7099    0  325 10094.9993 10094.7099  0.00%     -   20s  
     0     0 10094.7179    0  325 10094.9993 10094.7179  0.00%     -   20s  
     0     2 10094.7179    0  325 10094.9993 10094.7179  0.00%     -   20s  
H   96   114                    10094.990475 10094.7652  0.00%  85.7   21s  
H  249   294                    10094.987654 10094.7689  0.00%  61.1   21s  
*  262   294              36    10094.987402 10094.7689  0.00%  60.8   21s  
*  610   487              27    10094.987173 10094.7711  0.00%  46.5   21s  
H 3825   935                    10094.986106 10094.8707  0.00%  34.6   23s  
  
Cutting planes:  
  Gomory: 21  
  Cover: 18  
  Implied bound: 86  
  MIR: 98  
  Flow cover: 159  
  GUB cover: 1  
  RLT: 62  
  Relax-and-lift: 19  
  
Explored 4580 nodes (198808 simplex iterations) in 23.12 seconds (25.50 work units)  
Thread count was 20 (of 20 available processors)  
  
Solution count 10: 10095 10095 10095 ... 10095.4  
  
Optimal solution found (tolerance 1.00e-05)  
Best objective 1.009498610550e+04, best bound 1.009488620246e+04, gap 0.0010%  
.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,415 questions
Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,681 questions
.NET CLI
.NET CLI
A cross-platform toolchain for developing, building, running, and publishing .NET applications.
323 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,307 questions
{count} votes

7 answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 56,931 Reputation points
    2022-11-03T19:10:57.987+00:00

    trival example:

    using System.Runtime.InteropServices;   
    using System.IO.Pipes;   
       
    namespace Test   
    {   
        class Program   
        {   
            [DllImport("Kernel32.dll", SetLastError = true) ]   
            static extern int SetStdHandle(int device, SafeHandle handle);    
       
            static void Main(string[] args)   
            {   
                // setup stdout pipe    
                var namedPipeServer = new AnonymousPipeServerStream(PipeDirection.In);   
                var streamReader = new StreamReader(namedPipeServer);   
                var namedPipClient = new AnonymousPipeClientStream(PipeDirection.Out, namedPipeServer.ClientSafePipeHandle);   
                var status = SetStdHandle(-11, namedPipClient.SafePipeHandle); // set stdout   
       
                // pipe write will not complete until pipe read, so setup async read   
                var readTask = streamReader.ReadLineAsync();   
       
                // write to stdout pipe   
                Console.WriteLine("Hello World!");     
       
                // display pipe output to std error   
                Console.Error.WriteLine(readTask.Result);   
            }   
        }   
    }   
    

    in a real program, you would use a background task read the pipe

    0 comments No comments

  2. Simon Rozman 1 Reputation point
    2023-09-06T10:19:42.6833333+00:00

    I stumbled across this thread when trying to achieve something similar in .NET. You can't use Console.SetOutput/SetError(), as you intercept .NET Console output only. You can't use PipedOutputStream because they have some internal handle management. I managed to intercept all stdout and stderr in-memory using CreatePipe and SetStdHandle calls.

    Here's the link to the complete code: https://github.com/Amebis/eduVPN/blob/master/eduVPN.Views/StandardStreamTracer.cs

    0 comments No comments