I would like to use dotnet trace as a profiler in BenchmarkDotNet (https://github.com/dotnet/BenchmarkDotNet/issues/1315)
But there is a problem. I have to run process and read output of this process.
```c#
using (Process p = new Process())
{
p.StartInfo.CreateNoWindow = false;
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.OutputDataReceived += (sender, args) => logger.WriteLine(LogKind.Default, args.Data);
p.ErrorDataReceived += (sender, args) => logger.WriteLine(LogKind.Error, args.Data);
p.StartInfo.FileName = "dotnet";
p.StartInfo.Arguments = $"trace collect --process-id {parameters.Process.Id} --output {GetFilePath(parameters, DateTime.Now)} --format Speedscope";
p.Start();
p.BeginOutputReadLine();
p.BeginErrorReadLine();
p.WaitForExit(180 * 1000);
if (!p.HasExited)
{
Debug.WriteLine("Killing process");
p.Kill();
}
}
After run I got the exception:
[ERROR] System.IO.IOException: The handle is invalid
at System.ConsolePal.GetBufferInfo(Boolean throwOnNoConsole, Boolean& succeeded)
at System.ConsolePal.Clear()
at Microsoft.Diagnostics.Tools.Trace.CollectCommandHandler.Collect(CancellationToken ct, IConsole console, Int32 processId, FileInfo output, UInt32 buffersize, String providers, String profile, TraceFileFormat format, TimeSpan duration) in /_/src/Tools/dotnet-trace/CommandLine/Commands/CollectCommand.cs:line 42
The exception was thrown here:
https://github.com/dotnet/diagnostics/blob/8338ba741cf256e5549f387c41b794f3ef57c609/src/Tools/dotnet-trace/CommandLine/Commands/CollectCommand.cs#L41
This line should be executed only if there is a console. E.g.:
```c#
if (GetConsoleWindow() != IntPtr.Zero) Console.Clear();
Is there a reason you are running dotnet-trace instead of just using the managed library? The library is available in a preview form today, but the productized version is almost finished (see: #617). I think this would be more straightforward and less cumbersome than shelling out to dotnet-trace. That being said, dotnet-trace _should_ work under redirection.
Closing as a dup of the issue #637 which was fixed in #644
Most helpful comment
Is there a reason you are running
dotnet-traceinstead of just using the managed library? The library is available in a preview form today, but the productized version is almost finished (see: #617). I think this would be more straightforward and less cumbersome than shelling out todotnet-trace. That being said,dotnet-trace_should_ work under redirection.