Avoid exceptions when console is not available (#644)
authorPaul Harrington <pharring@users.noreply.github.com>
Wed, 27 Nov 2019 18:41:29 +0000 (10:41 -0800)
committerJohn Salem <josalem@microsoft.com>
Wed, 27 Nov 2019 18:41:29 +0000 (10:41 -0800)
* Do not manipulate cursor if Console not available

* Use console.GetTerminal() to test for console

src/Tools/dotnet-trace/CommandLine/Commands/CollectCommand.cs

index fac9c13b1aac3845b1142328a3334fa23730193e..0776c50598d2b85287bc388033f477611a19d158 100644 (file)
@@ -38,7 +38,12 @@ namespace Microsoft.Diagnostics.Tools.Trace
             {
                 Debug.Assert(output != null);
                 Debug.Assert(profile != null);
-                Console.Clear();
+
+                bool hasConsole = console.GetTerminal() != null;
+
+                if (hasConsole)
+                    Console.Clear();
+
                 if (processId < 0)
                 {
                     Console.Error.WriteLine("Process ID should not be negative.");
@@ -141,8 +146,13 @@ namespace Microsoft.Diagnostics.Tools.Trace
                                     if (nBytesRead <= 0)
                                         break;
                                     fs.Write(buffer, 0, nBytesRead);
-                                    lineToClear = Console.CursorTop-1;
-                                    ResetCurrentConsoleLine(vTermMode.IsEnabled);
+
+                                    if (hasConsole)
+                                    {
+                                        lineToClear = Console.CursorTop - 1;
+                                        ResetCurrentConsoleLine(vTermMode.IsEnabled);
+                                    }
+
                                     Console.Out.WriteLine($"[{stopwatch.Elapsed.ToString(@"dd\:hh\:mm\:ss")}]\tRecording trace {GetSize(fs.Length)}");
                                     Console.Out.WriteLine("Press <Enter> or <Ctrl+C> to exit...");
                                     Debug.WriteLine($"PACKET: {Convert.ToBase64String(buffer, 0, nBytesRead)} (bytes {nBytesRead})");