From: Paul Harrington Date: Wed, 27 Nov 2019 18:41:29 +0000 (-0800) Subject: Avoid exceptions when console is not available (#644) X-Git-Tag: submit/tizen_5.5/20200504.045052~11^2^2~234 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=446d8c3800e3ee67fe7534d0e5e65c34d7ebf7b2;p=platform%2Fcore%2Fdotnet%2Fdiagnostics.git Avoid exceptions when console is not available (#644) * Do not manipulate cursor if Console not available * Use console.GetTerminal() to test for console --- diff --git a/src/Tools/dotnet-trace/CommandLine/Commands/CollectCommand.cs b/src/Tools/dotnet-trace/CommandLine/Commands/CollectCommand.cs index fac9c13b1..0776c5059 100644 --- a/src/Tools/dotnet-trace/CommandLine/Commands/CollectCommand.cs +++ b/src/Tools/dotnet-trace/CommandLine/Commands/CollectCommand.cs @@ -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 or to exit..."); Debug.WriteLine($"PACKET: {Convert.ToBase64String(buffer, 0, nBytesRead)} (bytes {nBytesRead})");