From 59b2a63009f8234ca0735e6b5045fd8f2fe17578 Mon Sep 17 00:00:00 2001 From: Brad Wilson Date: Wed, 17 Jul 2024 12:13:39 -0700 Subject: [PATCH] Catch formatting exceptions in LoggerHelper --- .../Utility/LoggerHelper.cs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/xunit.runner.visualstudio/Utility/LoggerHelper.cs b/src/xunit.runner.visualstudio/Utility/LoggerHelper.cs index 215bf76..2c99c14 100644 --- a/src/xunit.runner.visualstudio/Utility/LoggerHelper.cs +++ b/src/xunit.runner.visualstudio/Utility/LoggerHelper.cs @@ -1,3 +1,4 @@ +using System; using System.Diagnostics; using System.Globalization; using System.IO; @@ -61,7 +62,14 @@ void SendMessage( if (logger is null) return; - var assemblyText = assemblyName is null ? "" : $"{Path.GetFileNameWithoutExtension(assemblyName)}: "; - logger.SendMessage(level, $"[xUnit.net {Stopwatch.Elapsed:hh\\:mm\\:ss\\.ff}] {assemblyText}{string.Format(CultureInfo.CurrentCulture, format, args)}"); + try + { + var assemblyText = assemblyName is null ? "" : $"{Path.GetFileNameWithoutExtension(assemblyName)}: "; + logger.SendMessage(level, $"[xUnit.net {Stopwatch.Elapsed:hh\\:mm\\:ss\\.ff}] {assemblyText}{string.Format(CultureInfo.CurrentCulture, format, args)}"); + } + catch (Exception ex) + { + logger.SendMessage(TestMessageLevel.Warning, $"Exception formatting {level} message '{format}': {ex}"); + } } }