This question is different from the one proposed as a duplicate:
- My question is very specific to .net-core. I.e. even if there is a solution for "classic" .NET Framework it may not be applicable in the .NET Core environment.
- 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):
[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
[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?