Handle exception in System.Console.SetCursorPosition in some environments for dotnet...
authorHyungju Lee <leee.lee@samsung.com>
Wed, 8 Mar 2023 10:11:48 +0000 (19:11 +0900)
committerHyungju Lee <leee.lee@samsung.com>
Wed, 8 Mar 2023 11:15:02 +0000 (20:15 +0900)
src/Tools/Common/Commands/Utils.cs
src/Tools/dotnet-trace/CommandLine/Commands/CollectCommand.cs

index cb501fb3871c7b8826aa6640bdcff348df3d8390..75896863378a83165052c7e07330b7b6a43081cb 100644 (file)
@@ -160,6 +160,29 @@ namespace Microsoft.Internal.Common.Utils
         }
 
         private void SystemConsoleLineRewriter() => Console.SetCursorPosition(0, LineToClear);
+
+        private static bool? _isSetCursorPositionSupported;
+        public bool IsRewriteConsoleLineSupported
+        {
+            get {
+                bool isSupported = _isSetCursorPositionSupported ?? EnsureInitialized();
+                return isSupported;
+
+                bool EnsureInitialized()
+                {
+                    try {
+                        var left = Console.CursorLeft;
+                        var top = Console.CursorTop;
+                        Console.SetCursorPosition(0, LineToClear);
+                        Console.SetCursorPosition(left, top);
+                        _isSetCursorPositionSupported = true;
+                    } catch {
+                        _isSetCursorPositionSupported = false;
+                    }
+                    return (bool)_isSetCursorPositionSupported;
+                }
+            }
+        }
     }
 
     internal class ReturnCode
index c980e6d183d30d2360b2fbc36d6c7d7cac563dc1..e227b26a28ba01185f05845a36f67ce38b6afff7 100644 (file)
@@ -285,16 +285,23 @@ namespace Microsoft.Diagnostics.Tools.Trace
                             {
                                 rewriter = new LineRewriter { LineToClear = Console.CursorTop - 1 };
                                 Console.CursorVisible = false;
+                                if (!rewriter.IsRewriteConsoleLineSupported)
+                                {
+                                    ConsoleWriteLine("Recording trace in progress. Press <Enter> or <Ctrl+C> to exit...");
+                                }
                             }
 
                             Action printStatus = () =>
                             {
                                 if (printStatusOverTime)
                                 {
-                                    rewriter?.RewriteConsoleLine();
-                                    fileInfo.Refresh();
-                                    ConsoleWriteLine($"[{stopwatch.Elapsed.ToString(@"dd\:hh\:mm\:ss")}]\tRecording trace {GetSize(fileInfo.Length)}");
-                                    ConsoleWriteLine("Press <Enter> or <Ctrl+C> to exit...");
+                                    if (rewriter.IsRewriteConsoleLineSupported)
+                                    {
+                                        rewriter?.RewriteConsoleLine();
+                                        fileInfo.Refresh();
+                                        ConsoleWriteLine($"[{stopwatch.Elapsed.ToString(@"dd\:hh\:mm\:ss")}]\tRecording trace {GetSize(fileInfo.Length)}");
+                                        ConsoleWriteLine("Press <Enter> or <Ctrl+C> to exit...");
+                                    }
                                 }
 
                                 if (rundownRequested)