From 5d728706e47aa8f89944dd6e64b7e2bf689ad855 Mon Sep 17 00:00:00 2001 From: routersys Date: Sun, 6 Sep 2026 10:33:00 +0900 Subject: [PATCH] =?UTF-8?q?=E3=83=95=E3=83=AC=E3=83=BC=E3=83=A0=E3=82=92?= =?UTF-8?q?=E5=91=BC=E3=81=B3=E5=87=BA=E3=81=97=E5=85=83=E3=81=8B=E3=82=89?= =?UTF-8?q?=E9=80=81=E5=87=BA=E5=85=83=E3=81=AE=E9=A0=86=E3=81=AB=E4=B8=A6?= =?UTF-8?q?=E3=81=B9=E3=82=8B=E6=A7=8B=E9=80=A0=E5=8C=96=E3=82=92=E8=BF=BD?= =?UTF-8?q?=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Telemetry/ExceptionCapture.cs | 104 ++++++++++++++++++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 Telemetry/ExceptionCapture.cs diff --git a/Telemetry/ExceptionCapture.cs b/Telemetry/ExceptionCapture.cs new file mode 100644 index 0000000..764b7f0 --- /dev/null +++ b/Telemetry/ExceptionCapture.cs @@ -0,0 +1,104 @@ +using System.Diagnostics; +using System.Reflection; + +namespace Telemetry; + +internal static class ExceptionCapture +{ + public const int MaxChainLength = 5; + public const int MaxFrames = 100; + private const int NoLineNumber = 0; + + public static IReadOnlyList Capture(Exception exception, Assembly? ownAssembly) + { + ArgumentNullException.ThrowIfNull(exception); + + var thrownFirst = new List(MaxChainLength); + var current = exception; + + while (current is not null && thrownFirst.Count < MaxChainLength) + { + thrownFirst.Add(current); + current = current is AggregateException aggregate && aggregate.InnerExceptions.Count > 0 + ? aggregate.InnerExceptions[0] + : current.InnerException; + } + + var captured = new List(thrownFirst.Count); + for (var index = thrownFirst.Count - 1; index >= 0; index--) + { + captured.Add(CaptureSingle(thrownFirst[index], ownAssembly)); + } + + return captured; + } + + private static CapturedException CaptureSingle(Exception exception, Assembly? ownAssembly) => new( + exception.GetType().FullName ?? exception.GetType().Name, + TextScrubber.Scrub(exception.Message) ?? string.Empty, + CaptureFrames(exception, ownAssembly)); + + private static IReadOnlyList CaptureFrames(Exception exception, Assembly? ownAssembly) + { + StackFrame[]? frames; + + try + { + frames = new StackTrace(exception, true).GetFrames(); + } + catch (Exception) + { + return []; + } + + if (frames is null || frames.Length == 0) + { + return []; + } + + var captured = new List(Math.Min(frames.Length, MaxFrames)); + + for (var index = Math.Min(frames.Length, MaxFrames) - 1; index >= 0; index--) + { + var frame = CaptureFrame(frames[index], ownAssembly); + if (frame is not null) + { + captured.Add(frame); + } + } + + return captured; + } + + private static CapturedFrame? CaptureFrame(StackFrame frame, Assembly? ownAssembly) + { + MethodBase? method; + + try + { + method = frame.GetMethod(); + } + catch (Exception) + { + method = null; + } + + var function = method?.Name; + var fileName = TextScrubber.Scrub(frame.GetFileName()); + + if (string.IsNullOrEmpty(function) && string.IsNullOrEmpty(fileName)) + { + return null; + } + + var declaringType = method?.DeclaringType; + var lineNumber = frame.GetFileLineNumber(); + + return new CapturedFrame( + declaringType?.FullName, + function, + fileName, + lineNumber > NoLineNumber ? lineNumber : NoLineNumber, + ownAssembly is not null && declaringType is not null && ReferenceEquals(declaringType.Assembly, ownAssembly)); + } +}