From ca076c89951b2f857dd2a1fcaacd30e0c021b98b Mon Sep 17 00:00:00 2001 From: routersys Date: Sun, 25 Oct 2026 11:42:48 +0900 Subject: [PATCH] =?UTF-8?q?=E3=83=86=E3=83=AC=E3=83=A1=E3=83=88=E3=83=AA?= =?UTF-8?q?=E3=82=92=E8=BF=BD=E5=8A=A0=E3=81=99=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../PartialInnerOutlineTelemetry.cs | 102 ++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 PartialInnerOutline/PartialInnerOutlineTelemetry.cs diff --git a/PartialInnerOutline/PartialInnerOutlineTelemetry.cs b/PartialInnerOutline/PartialInnerOutlineTelemetry.cs new file mode 100644 index 0000000..ed538b9 --- /dev/null +++ b/PartialInnerOutline/PartialInnerOutlineTelemetry.cs @@ -0,0 +1,102 @@ +using System.Diagnostics; +using System.Reflection; +using System.Windows; +using System.Windows.Threading; +using Telemetry; + +namespace PartialInnerOutline; + +internal static class PartialInnerOutlineTelemetry +{ + private const int MaxChainLength = 5; + private const int MaxFrames = 100; + + private static readonly Assembly Own = typeof(PartialInnerOutlineTelemetry).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; + } +}