From 4f1ee5385c0f542f3279a1656bd3856fad0819dc Mon Sep 17 00:00:00 2001 From: Peter Kirmeier Date: Mon, 11 Sep 2023 17:50:10 +0200 Subject: [PATCH] Log spam protection on COM exceptions while reading icons --- Utilities/File/IconReader.cs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/Utilities/File/IconReader.cs b/Utilities/File/IconReader.cs index 4827697..2016842 100644 --- a/Utilities/File/IconReader.cs +++ b/Utilities/File/IconReader.cs @@ -27,6 +27,8 @@ namespace SystemTrayMenu.Utilities private static readonly ConcurrentDictionary IconDictCache = new(); private static readonly BitmapSource? OverlayImage = (BitmapSource?)Application.Current.Resources["LinkArrowIconImage"]; + private static int maxCOMExceptionLogs = 5; + // see https://github.com/Hofknecht/SystemTrayMenu/issues/209. internal static bool IsPreloading { get; set; } = true; @@ -203,7 +205,12 @@ namespace SystemTrayMenu.Utilities catch (COMException ex) { // This seems to happen very rarely, so we just log it and go on - Log.Warn("Reading native Icon failed:", ex); + // However, we protect this log message from being called often, so we stop logging after a while + if (maxCOMExceptionLogs > 0) + { + maxCOMExceptionLogs--; // Note: written by multiple threads, but it is not worth any protection + Log.Warn("Reading native Icon failed:", ex); + } } icon?.Dispose();