|
| 1 | +using Sentry.Infrastructure; |
| 2 | + |
| 3 | +namespace Sentry.Extensibility; |
| 4 | + |
| 5 | +#if NET8_0_OR_GREATER |
| 6 | + |
| 7 | +/// <summary> |
| 8 | +/// A rudimentary implementation of <see cref="ISentryStackTraceFactory"/> that simply parses the |
| 9 | +/// string representation of the stack trace from an exception. This lacks many of the features |
| 10 | +/// off the full <see cref="SentryStackTraceFactory"/>. However, it may be useful in AOT compiled |
| 11 | +/// applications where the full factory is not returning a useful stack trace. |
| 12 | +/// <remarks> |
| 13 | +/// <para> |
| 14 | +/// This class is currently EXPERIMENTAL |
| 15 | +/// </para> |
| 16 | +/// <para> |
| 17 | +/// This factory is designed for AOT scenarios, so only available for net8.0+ |
| 18 | +/// </para> |
| 19 | +/// </remarks> |
| 20 | +/// </summary> |
| 21 | +[Experimental(DiagnosticId.ExperimentalFeature)] |
| 22 | +public partial class StringStackTraceFactory : ISentryStackTraceFactory |
| 23 | +{ |
| 24 | + private readonly SentryOptions _options; |
| 25 | + private const string FullStackTraceLinePattern = @"at (?<Module>[^\.]+)\.(?<Function>.*?) in (?<FileName>.*?):line (?<LineNo>\d+)"; |
| 26 | + private const string StackTraceLinePattern = @"at (.+)\.(.+) \+"; |
| 27 | + |
| 28 | +#if NET9_0_OR_GREATER |
| 29 | + [GeneratedRegex(FullStackTraceLinePattern)] |
| 30 | + internal static partial Regex FullStackTraceLine { get; } |
| 31 | +#else |
| 32 | + internal static readonly Regex FullStackTraceLine = FullStackTraceLineRegex(); |
| 33 | + |
| 34 | + [GeneratedRegex(FullStackTraceLinePattern)] |
| 35 | + private static partial Regex FullStackTraceLineRegex(); |
| 36 | +#endif |
| 37 | + |
| 38 | +#if NET9_0_OR_GREATER |
| 39 | + [GeneratedRegex(StackTraceLinePattern)] |
| 40 | + private static partial Regex StackTraceLine { get; } |
| 41 | +#else |
| 42 | + private static readonly Regex StackTraceLine = StackTraceLineRegex(); |
| 43 | + |
| 44 | + [GeneratedRegex(StackTraceLinePattern)] |
| 45 | + private static partial Regex StackTraceLineRegex(); |
| 46 | +#endif |
| 47 | + |
| 48 | + /// <summary> |
| 49 | + /// Creates a new instance of <see cref="StringStackTraceFactory"/>. |
| 50 | + /// </summary> |
| 51 | + /// <param name="options">The sentry options</param> |
| 52 | + public StringStackTraceFactory(SentryOptions options) |
| 53 | + { |
| 54 | + _options = options; |
| 55 | + } |
| 56 | + |
| 57 | + /// <inheritdoc /> |
| 58 | + public SentryStackTrace? Create(Exception? exception = null) |
| 59 | + { |
| 60 | + _options.LogDebug("Source Stack Trace: {0}", exception?.StackTrace); |
| 61 | + |
| 62 | + var trace = new SentryStackTrace(); |
| 63 | + var frames = new List<SentryStackFrame>(); |
| 64 | + |
| 65 | + var lines = exception?.StackTrace?.Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries) ?? []; |
| 66 | + foreach (var line in lines) |
| 67 | + { |
| 68 | + var fullMatch = FullStackTraceLine.Match(line); |
| 69 | + if (fullMatch.Success) |
| 70 | + { |
| 71 | + frames.Add(new SentryStackFrame() |
| 72 | + { |
| 73 | + Module = fullMatch.Groups[1].Value, |
| 74 | + Function = fullMatch.Groups[2].Value, |
| 75 | + FileName = fullMatch.Groups[3].Value, |
| 76 | + LineNumber = int.Parse(fullMatch.Groups[4].Value), |
| 77 | + }); |
| 78 | + continue; |
| 79 | + } |
| 80 | + |
| 81 | + _options.LogDebug("Full stack frame match failed for: {0}", line); |
| 82 | + var lineMatch = StackTraceLine.Match(line); |
| 83 | + if (lineMatch.Success) |
| 84 | + { |
| 85 | + frames.Add(new SentryStackFrame() |
| 86 | + { |
| 87 | + Module = lineMatch.Groups[1].Value, |
| 88 | + Function = lineMatch.Groups[2].Value |
| 89 | + }); |
| 90 | + continue; |
| 91 | + } |
| 92 | + |
| 93 | + _options.LogDebug("Stack frame match failed for: {0}", line); |
| 94 | + frames.Add(new SentryStackFrame() |
| 95 | + { |
| 96 | + Function = line |
| 97 | + }); |
| 98 | + } |
| 99 | + |
| 100 | + trace.Frames = frames; |
| 101 | + _options.LogDebug("Created {0} with {1} frames.", "StringStackTrace", trace.Frames.Count); |
| 102 | + return trace.Frames.Count != 0 ? trace : null; |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +#endif |
0 commit comments