Do not swallow exception and stuck in case we have an exception thrown during tracing
authorAndrew Au <andrewau@microsoft.com>
Thu, 30 May 2019 00:26:49 +0000 (17:26 -0700)
committerAndrew Au <cshung@gmail.com>
Thu, 30 May 2019 01:23:53 +0000 (18:23 -0700)
src/Tools/dotnet-counters/CounterMonitor.cs
src/Tools/dotnet-trace/CommandLine/Commands/CollectCommand.cs

index d88809fee568cf7724e706290e3fbe4100736c4f..ffda06f9da12aa34b10370c0e79251330ded0877 100644 (file)
@@ -206,7 +206,7 @@ namespace Microsoft.Diagnostics.Tools.Counters
                 {
                     EventPipeClient.StopTracing(_processId, _sessionId);    
                 }
-                catch (System.IO.EndOfStreamException) {} // If the app we're monitoring exits abrubtly, this may throw in which case we just swallow the exception and exit gracefully.    
+                catch (System.IO.EndOfStreamException) {} // If the app we're monitoring exits abruptly, this may throw in which case we just swallow the exception and exit gracefully.
             }
             
             return 0;
index 6d42a03ddbfa83a914c2ff32a7b3eebd81f6ee7a..00bd26c27ebcedc36d33ae0718443c72bf656820 100644 (file)
@@ -99,26 +99,36 @@ namespace Microsoft.Diagnostics.Tools.Trace
                     }
 
                     var collectingTask = new Task(() => {
-                        using (var fs = new FileStream(output.FullName, FileMode.Create, FileAccess.Write))
+                        try
                         {
-                            Console.Out.WriteLine($"Process     : {process.MainModule.FileName}");
-                            Console.Out.WriteLine($"Output File : {fs.Name}");
-                            Console.Out.WriteLine($"\tSession Id: 0x{sessionId:X16}");
-                            lineToClear = Console.CursorTop;
-
-                            while (true)
+                            using (var fs = new FileStream(output.FullName, FileMode.Create, FileAccess.Write))
                             {
-                                var buffer = new byte[16 * 1024];
-                                int nBytesRead = stream.Read(buffer, 0, buffer.Length);
-                                if (nBytesRead <= 0)
-                                    break;
-                                fs.Write(buffer, 0, nBytesRead);
-
-                                ResetCurrentConsoleLine(vTermMode.IsEnabled);
-                                Console.Out.Write($"\tRecording trace {GetSize(fs.Length)}");
-
-                                Debug.WriteLine($"PACKET: {Convert.ToBase64String(buffer, 0, nBytesRead)} (bytes {nBytesRead})");
+                                Console.Out.WriteLine($"Process     : {process.MainModule.FileName}");
+                                Console.Out.WriteLine($"Output File : {fs.Name}");
+                                Console.Out.WriteLine($"\tSession Id: 0x{sessionId:X16}");
+                                lineToClear = Console.CursorTop;
+
+                                while (true)
+                                {
+                                    var buffer = new byte[16 * 1024];
+                                    int nBytesRead = stream.Read(buffer, 0, buffer.Length);
+                                    if (nBytesRead <= 0)
+                                        break;
+                                    fs.Write(buffer, 0, nBytesRead);
+
+                                    ResetCurrentConsoleLine(vTermMode.IsEnabled);
+                                    Console.Out.Write($"\tRecording trace {GetSize(fs.Length)}");
+
+                                    Debug.WriteLine($"PACKET: {Convert.ToBase64String(buffer, 0, nBytesRead)} (bytes {nBytesRead})");
+                                }
                             }
+                        }
+                        catch (Exception ex)
+                        {
+                            Console.Error.WriteLine($"[ERROR] {ex.ToString()}");
+                        }
+                        finally
+                        {
                             terminated = true;
                             shouldExit.Set();
                         }