question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Redirect stdout from loaded assembly console.writeline

See original GitHub issue

Prerequisites

Steps to reproduce

Doubt if it’s a bug, limitation or a lack of enough knowledge: I’m loading arbitrary .NET assemblies (which I have no control of their source code) and invoke them. A lot of them are using console.writeline (or similar functions), and I want to capture it, means redirecting it to a Powershell variable or even to a file, but nothing seems to catch this - it’s just printing it to the console. Tried about million ways to overcome this unsuccessfully (> file.txt , out-string, out-variable, and so on…) here is the code:

$file = "path\to\my\compiled\assembly"
$bytes = [System.IO.File]::ReadAllBytes($file)
$assembly = [Reflection.Assembly]::Load($bytes)
$params = @(,[String[]]@("some params"))
$assembly.EntryPoint.invoke($null, $params)  # <-- I want to catch this output, into a variable or a file, but it keeps be printed to the screen.

Expected behavior

PS> $x = $assembly.EntryPoint.invoke($null, $params) | out-string
PS> $x
output

Actual behavior

PS> $x = $assembly.EntryPoint.invoke($null, $params) | out-string
PS> $x
nothing

Error details

No response

Environment data

Name                           Value                                                                                                                                                                                           
----                           -----                                                                                                                                                                                           
PSVersion                      5.1.19041.1682                                                                                                                                                                                  
PSEdition                      Desktop                                                                                                                                                                                         
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}                                                                                                                                                                         
BuildVersion                   10.0.19041.1682                                                                                                                                                                                 
CLRVersion                     4.0.30319.42000                                                                                                                                                                                 
WSManStackVersion              3.0                                                                                                                                                                                             
PSRemotingProtocolVersion      2.3                                                                                                                                                                                             
SerializationVersion           1.1.0.1

Visuals

No response

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
MartinGC94commented, Oct 24, 2022

You can also set the console output to a stream like this:

$Writer = [System.IO.StreamWriter]::new("$HOME\DemoLog.txt")
$Writer.AutoFlush = $true
$OriginalOut = [System.Console]::Out
try
{
    [System.Console]::SetOut($Writer)
    [System.Console]::WriteLine("Hello")
}
finally
{
    [System.Console]::SetOut($OriginalOut)
}
0reactions
itaymigdalcommented, Oct 25, 2022

@MartinGC94 Seems like it’s working, great catch! Thank you so much 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Redirect Console.Write of specific assembly
1 Answer 1 · Console class has static method SetOut , which redirects output to StreamWriter object. · Write your own StreamWriter extension...
Read more >
How to redirect/catch Console.WriteLine() output
Dear allIn my script I load the .NET assembly library "TagLib#", a library to access e.g. mp3-tags.If the the importend "Create"-function of ...
Read more >
Customize output in hosted CLR (.NET) - Microsoft Q&A
Console.WriteLine uses the processes standard output file handle. so to capture all standard output for your process, you redirect standard ...
Read more >
How to redirect standard output for a .NET console application
The Console. SetOut method is the most important method to remember if you want to redirect the standard output.
Read more >
Console.SetOut(TextWriter) Method (System)
It replaces four consecutive space characters in a string with a tab character. To run it, you must supply two command line arguments....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found