From c8c5f57c89204034c0812c4aa366d3eb30bf935e Mon Sep 17 00:00:00 2001 From: routersys Date: Tue, 22 Sep 2026 12:38:00 +0900 Subject: [PATCH] =?UTF-8?q?InkDispersionTelemetry=E3=82=92=E8=BF=BD?= =?UTF-8?q?=E5=8A=A0=E3=81=99=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- InkDispersion/InkDispersionEffect.cs | 1 + InkDispersion/InkDispersionTelemetry.cs | 102 ++++++++++++++++++++++++ 2 files changed, 103 insertions(+) create mode 100644 InkDispersion/InkDispersionTelemetry.cs diff --git a/InkDispersion/InkDispersionEffect.cs b/InkDispersion/InkDispersionEffect.cs index 56a1870..ba8a2b2 100644 --- a/InkDispersion/InkDispersionEffect.cs +++ b/InkDispersion/InkDispersionEffect.cs @@ -16,6 +16,7 @@ public sealed class InkDispersionEffect : VideoEffectBase public InkDispersionEffect() { + InkDispersionTelemetry.EnsureStartedOnce(); InkDispersionUpdateNotifier.EnsureCheckedOnce(); } diff --git a/InkDispersion/InkDispersionTelemetry.cs b/InkDispersion/InkDispersionTelemetry.cs new file mode 100644 index 0000000..7c0bb22 --- /dev/null +++ b/InkDispersion/InkDispersionTelemetry.cs @@ -0,0 +1,102 @@ +using System.Diagnostics; +using System.Reflection; +using System.Windows; +using System.Windows.Threading; +using Telemetry; + +namespace InkDispersion; + +internal static class InkDispersionTelemetry +{ + private const int MaxChainLength = 5; + private const int MaxFrames = 100; + + private static readonly Assembly Own = typeof(InkDispersionTelemetry).Assembly; + private static int _started; + + public static void EnsureStartedOnce() + { + var application = Application.Current; + if (application is null || Interlocked.Exchange(ref _started, 1) != 0) + return; + + AppDomain.CurrentDomain.UnhandledException += OnUnhandledException; + TaskScheduler.UnobservedTaskException += OnUnobservedTaskException; + application.DispatcherUnhandledException += OnDispatcherUnhandledException; + + TelemetryReporter.Start(); + } + + public static void Report(Exception exception) + { + if (Volatile.Read(ref _started) != 0) + TelemetryReporter.Report(exception); + } + + private static void OnUnhandledException(object sender, UnhandledExceptionEventArgs e) + => ReportOwn(e.ExceptionObject as Exception); + + private static void OnUnobservedTaskException(object? sender, UnobservedTaskExceptionEventArgs e) + => ReportOwn(e.Exception); + + private static void OnDispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e) + => ReportOwn(e.Exception); + + private static void ReportOwn(Exception? exception) + { + if (exception is not null && IsOwn(exception)) + TelemetryReporter.Report(exception); + } + + private static bool IsOwn(Exception exception) + { + var current = exception; + for (var depth = 0; current is not null && depth < MaxChainLength; depth++) + { + if (HasOwnFrame(current)) + return true; + + current = current is AggregateException aggregate && aggregate.InnerExceptions.Count > 0 + ? aggregate.InnerExceptions[0] + : current.InnerException; + } + + return false; + } + + private static bool HasOwnFrame(Exception exception) + { + StackFrame[]? frames; + + try + { + frames = new StackTrace(exception, false).GetFrames(); + } + catch (Exception) + { + return false; + } + + if (frames is null) + return false; + + for (var index = 0; index < Math.Min(frames.Length, MaxFrames); index++) + { + MethodBase? method; + + try + { + method = frames[index].GetMethod(); + } + catch (Exception) + { + continue; + } + + if (method?.DeclaringType is { } declaringType && ReferenceEquals(declaringType.Assembly, Own)) + return true; + } + + return false; + } +}