From f6097d99417ab2397a4ebebce381abee527b3228 Mon Sep 17 00:00:00 2001 From: Byron Mayne Date: Sun, 21 Jan 2024 17:53:07 -0500 Subject: [PATCH] Suppressed a COMException which can happen when debugging This would be caused because Visual Studio is currently on a break point and the log can't be produced. --- .../VisualStudio/VisualStudioLogEventSink.cs | 24 ++++++++++++------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/src/Plugins/SourceGenerator.Foundations.Windows/Interop/VisualStudio/VisualStudioLogEventSink.cs b/src/Plugins/SourceGenerator.Foundations.Windows/Interop/VisualStudio/VisualStudioLogEventSink.cs index ce0e7f4..32160fe 100644 --- a/src/Plugins/SourceGenerator.Foundations.Windows/Interop/VisualStudio/VisualStudioLogEventSink.cs +++ b/src/Plugins/SourceGenerator.Foundations.Windows/Interop/VisualStudio/VisualStudioLogEventSink.cs @@ -1,6 +1,7 @@ using EnvDTE; using SGF.Diagnostics; using System; +using System.Runtime.InteropServices; using Constants = EnvDTE.Constants; namespace SGF.Interop.VisualStudio @@ -96,10 +97,10 @@ public void Write(LogLevel logLevel, string message) if (logLevel >= LogLevel.Warning) { - m_buildOutput?.OutputString(message); + Write(message); } - m_sourceGeneratorOutput?.OutputString(message); + Write(message); } @@ -108,17 +109,24 @@ public void Write(LogLevel logLevel, string message) /// public void Write(string message) { - m_sourceGeneratorOutput?.OutputString(message); + try + { + m_sourceGeneratorOutput?.OutputString(message); + } + catch (COMException) + { + // When the person is debugging Visual Studio will be busy and this will throw this exception + } + catch(Exception exception) + { + Console.WriteLine($"Exception was thrown while writing log. Please report this on gihub {exception}"); + } } - - /// /// Writes an entry to the output window if it has been initialized /// public void WriteLine(string message) - { - m_sourceGeneratorOutput?.OutputString($"{message}{Environment.NewLine}"); - } + => Write($"{message}{Environment.NewLine}"); } } \ No newline at end of file