Skip to content

Run dotnet trace as process without console #645

Closed

Description

@WojciechNagorski

I would like to use dotnet trace as a profiler in BenchmarkDotNet (dotnet/BenchmarkDotNet#1315)

But there is a problem. I have to run process and read output of this process.

                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:


This line should be executed only if there is a console. E.g.:

 if (GetConsoleWindow() != IntPtr.Zero) Console.Clear();

Activity

  1. added
    bug
    on Nov 25, 2019
  2. josalem commented on Nov 25, 2019

    @josalem
    Contributor

    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.

  3. josalem commented on Nov 27, 2019

    @josalem
    Contributor

    Closing as a dup of the issue #637 which was fixed in #644

  4. ghost locked as resolved and limited conversation to collaborators on Jun 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugdotnet-trace

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions