2

This question is different from the one proposed as a duplicate:

  1. My question is very specific to . I.e. even if there is a solution for "classic" .NET Framework it may not be applicable in the .NET Core environment.
  2. There's a significant (8 years!) time gap between the questions posted.

Please do not treat it as a duplicate, because it isn't.


I found myself unable to tap on stderr and stdout streams of processes which were created outside of the process of my application.

targetProcessList is of type List<System.Diagnostics.Process>.


The following code

foreach (var proc in targetProcessList)
{
    proc.StandardError.ReadToEndAsync().ContinueWith(t => Console.WriteLine(t.Result));
    proc.StandardOutput.ReadToEndAsync().ContinueWith(t => Console.WriteLine(t.Result));
}

results in an InvalidOperationException (perhaps because the Process isn't configured for such usage):

enter image description here

[c:\           HRIS.Web] dotnet run :       User profile is available. Using '                 \AppData\Local\ASP.NET\DataProtection-Keys' as key repository and Windows DPAPI to encrypt keys at rest.
[c:\           HRIS.Web] dotnet run :
[c:\           HRIS.SPA] npm run start-prodish-for-e2e ||| [12044] cmd
[c:\           HRIS.SPA] npm run start-prodish-for-e2e ||| [11080] conhost
System.InvalidOperationException: StandardError has not been redirected.
   at System.Diagnostics.Process.get_StandardError()
   at RunE2E.Program.StartProcessViaCmd(String command, String arguments, String workingDirectory)
   at RunE2E.Program.Main(String[] args)
System.NullReferenceException: Object reference not set to an instance of an object.
   at RunE2E.Program.Main(String[] args)

So, I'm trying to dynamically enable the redirection like this:

foreach (var proc in targetProcessList)
{
    proc.StartInfo.RedirectStandardError = true;
    proc.StartInfo.RedirectStandardOutput = true;
    proc.StandardError.ReadToEndAsync().ContinueWith(t => Console.WriteLine(t.Result));
    proc.StandardOutput.ReadToEndAsync().ContinueWith(t => Console.WriteLine(t.Result));
}

but still getting slapped at run time

enter image description here

[c:\           \HRIS.Web] dotnet run :       User profile is available. Using '                 \AppData\Local\ASP.NET\DataProtection-Keys' as key repository and Windows DPAPI to encrypt keys at rest.
[c:\           \HRIS.Web] dotnet run :
[c:\           \HRIS.SPA] npm run start-prodish-for-e2e ||| [14952] cmd
[c:\           \HRIS.SPA] npm run start-prodish-for-e2e ||| [10784] conhost
System.InvalidOperationException: Process was not started by this object, so requested information cannot be determined.
   at System.Diagnostics.Process.get_StartInfo()
   at RunE2E.Program.StartProcessViaCmd(String command, String arguments, String workingDirectory)
   at RunE2E.Program.Main(String[] args)
System.NullReferenceException: Object reference not set to an instance of an object.
   at RunE2E.Program.Main(String[] args)

Is there a way to work around that?

Igor Soloydenko
  • 11,067
  • 11
  • 47
  • 90
  • Are you creating these processes in code? Can you post that as well. – A G May 01 '18 at 06:42
  • 2
    @AseemGautam the very first sentence of my question says: "of processes which were created OUTSIDE of the process of my application" – Igor Soloydenko May 01 '18 at 15:37
  • This has been asked - https://stackoverflow.com/questions/2095826/c-redirect-standard-output-of-a-process-that-is-already-running - no solution though. – A G May 01 '18 at 18:37
  • Possible duplicate of [C#: Redirect Standard Output of a Process that is Already Running](https://stackoverflow.com/questions/2095826/c-redirect-standard-output-of-a-process-that-is-already-running) – A G May 01 '18 at 18:37
  • @AseemGautam The question you are referring to is VERY outdated (2010) and may or may not be applicable to .NET Core environment specifically. – Igor Soloydenko May 01 '18 at 18:39

0 Answers0