Skip to content

Commit 446d8c3

Browse files
pharringJohn Salem
authored andcommitted
Avoid exceptions when console is not available (#644)
* Do not manipulate cursor if Console not available * Use console.GetTerminal() to test for console
1 parent 51d3b88 commit 446d8c3

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

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

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,12 @@ private static async Task<int> Collect(CancellationToken ct, IConsole console, i
3838
{
3939
Debug.Assert(output != null);
4040
Debug.Assert(profile != null);
41-
Console.Clear();
41+
42+
bool hasConsole = console.GetTerminal() != null;
43+
44+
if (hasConsole)
45+
Console.Clear();
46+
4247
if (processId < 0)
4348
{
4449
Console.Error.WriteLine("Process ID should not be negative.");
@@ -141,8 +146,13 @@ private static async Task<int> Collect(CancellationToken ct, IConsole console, i
141146
if (nBytesRead <= 0)
142147
break;
143148
fs.Write(buffer, 0, nBytesRead);
144-
lineToClear = Console.CursorTop-1;
145-
ResetCurrentConsoleLine(vTermMode.IsEnabled);
149+
150+
if (hasConsole)
151+
{
152+
lineToClear = Console.CursorTop - 1;
153+
ResetCurrentConsoleLine(vTermMode.IsEnabled);
154+
}
155+
146156
Console.Out.WriteLine($"[{stopwatch.Elapsed.ToString(@"dd\:hh\:mm\:ss")}]\tRecording trace {GetSize(fs.Length)}");
147157
Console.Out.WriteLine("Press <Enter> or <Ctrl+C> to exit...");
148158
Debug.WriteLine($"PACKET: {Convert.ToBase64String(buffer, 0, nBytesRead)} (bytes {nBytesRead})");

0 commit comments

Comments
 (0)