From 518ab6464418cb1912ae4adad08262c440f4da42 Mon Sep 17 00:00:00 2001 From: Bruno Garcia Date: Mon, 25 Jun 2018 20:57:41 +0200 Subject: [PATCH] Prototype source with event --- NuGet.Config | 7 +++ Sentry.sln | 7 +-- src/Sentry/Protocol/SentryEvent.cs | 73 +++++++++++++++++++----------- src/Sentry/Sentry.csproj | 10 ++++ 4 files changed, 68 insertions(+), 29 deletions(-) create mode 100644 NuGet.Config diff --git a/NuGet.Config b/NuGet.Config new file mode 100644 index 0000000000..2ac6645e63 --- /dev/null +++ b/NuGet.Config @@ -0,0 +1,7 @@ + + + + + + + diff --git a/Sentry.sln b/Sentry.sln index 52c588d906..f18f0447d0 100644 --- a/Sentry.sln +++ b/Sentry.sln @@ -19,18 +19,19 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Sentry.Tests", "test\Sentry EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "1 - Solution Items", "1 - Solution Items", "{9D7E2F87-D6F4-4BBB-8980-87D0A8344C74}" ProjectSection(SolutionItems) = preProject + .appveyor.yml = .appveyor.yml .editorconfig = .editorconfig .gitattributes = .gitattributes .gitignore = .gitignore + .travis.yml = .travis.yml after.Sentry.sln.targets = after.Sentry.sln.targets build.ps1 = build.ps1 build.sh = build.sh Directory.Build.props = Directory.Build.props Directory.Build.targets = Directory.Build.targets global.json = global.json + NuGet.Config = NuGet.Config README.md = README.md - .appveyor.yml = .appveyor.yml - .travis.yml = .travis.yml EndProjectSection EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "samples", "samples", "{77454495-55EE-4B40-A089-71B9E8F82E89}" @@ -63,7 +64,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Sentry.AspNetCore.Tests", " EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Sentry.Extensions.Logging.Tests", "test\Sentry.Extensions.Logging.Tests\Sentry.Extensions.Logging.Tests.csproj", "{32B17E4A-8C30-44CC-B1D3-093CF284B1C3}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sentry.Testing", "test\Sentry.Testing\Sentry.Testing.csproj", "{20602A5A-B413-4E48-9AC9-00A7155774BD}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Sentry.Testing", "test\Sentry.Testing\Sentry.Testing.csproj", "{20602A5A-B413-4E48-9AC9-00A7155774BD}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution diff --git a/src/Sentry/Protocol/SentryEvent.cs b/src/Sentry/Protocol/SentryEvent.cs index ccf0e3df71..8d7ef0e885 100644 --- a/src/Sentry/Protocol/SentryEvent.cs +++ b/src/Sentry/Protocol/SentryEvent.cs @@ -4,9 +4,12 @@ using System.ComponentModel; using System.Diagnostics; using System.Linq; +using System.Reflection; using System.Runtime.Serialization; using System.Text.RegularExpressions; using System.Threading; +using Microsoft.Extensions.FileProviders; +using Microsoft.Extensions.StackTrace.Sources; using Sentry.Infrastructure; using Sentry.Protocol; @@ -135,6 +138,11 @@ internal SentryEvent( } } + + // TODO: This is a hack to test this stuff + private static IFileProvider Provider = new CompositeFileProvider(new EmbeddedFileProvider(Assembly.GetEntryAssembly())); + private static ExceptionDetailsProvider DetailsProvider = new ExceptionDetailsProvider(Provider, 10); + private void Populate(Exception exception) { // TODO: should this be dotnet instead? @@ -157,7 +165,6 @@ private void Populate(Exception exception) } InternalModules = builder.ToImmutable(); - if (exception != null) { var sentryExceptions = CreateSentryException(exception) @@ -173,6 +180,7 @@ private static IEnumerable CreateSentryException(Exception exce { Debug.Assert(exception != null); + // TODO: Replace it with: ExceptionDetailsProvider.FlattenAndReverseExceptionTree if (exception is AggregateException ae) { foreach (var inner in ae.InnerExceptions.SelectMany(CreateSentryException)) @@ -197,14 +205,12 @@ private static IEnumerable CreateSentryException(Exception exce Mechanism = GetMechanism(exception) }; - - var stackTrace = new StackTrace(exception, true); - - // Sentry expects the frames to be sent in reversed order - var frames = stackTrace.GetFrames() + var frames = StackTraceHelper.GetFrames(exception) + // Sentry expects the frames to be sent in reversed order ?.Reverse() .Select(CreateSentryStackFrame) - .ToList(); + .ToArray(); + if (frames != null) { @@ -247,47 +253,62 @@ public static Mechanism GetMechanism(Exception exception) return mechanism; } - public static SentryStackFrame CreateSentryStackFrame(StackFrame stackFrame) + internal static SentryStackFrame CreateSentryStackFrame(StackFrameInfo frameInfo) { const string unknownRequiredField = "(unknown)"; - var frame = new SentryStackFrame(); + var sentryFrame = new SentryStackFrame(); - if (stackFrame.HasMethod()) + var frame = frameInfo.StackFrame; + if (frame.HasMethod()) { - var method = stackFrame.GetMethod(); + var method = frame.GetMethod(); // TODO: SentryStackFrame.TryParse and skip frame instead of these unknown values: - frame.Module = method.DeclaringType?.FullName ?? unknownRequiredField; - frame.Package = method.DeclaringType?.Assembly.FullName; - frame.Function = method.Name; - frame.ContextLine = method.ToString(); + sentryFrame.Module = method.DeclaringType?.FullName ?? unknownRequiredField; + sentryFrame.Package = method.DeclaringType?.Assembly.FullName; + sentryFrame.Function = frameInfo.MethodDisplayInfo.ToString(); + sentryFrame.ContextLine = method.ToString(); } - frame.InApp = !IsSystemModuleName(frame.Module); - frame.FileName = stackFrame.GetFileName(); + sentryFrame.InApp = !IsSystemModuleName(sentryFrame.Module); + sentryFrame.AbsolutePath = frame.GetFileName(); - if (stackFrame.HasILOffset()) + if (frame.HasILOffset()) { - frame.InstructionOffset = stackFrame.GetILOffset(); + sentryFrame.InstructionOffset = frame.GetILOffset(); } - var lineNo = stackFrame.GetFileLineNumber(); + var lineNo = frame.GetFileLineNumber(); if (lineNo != 0) { - frame.LineNumber = lineNo; + sentryFrame.LineNumber = lineNo; } - var colNo = stackFrame.GetFileColumnNumber(); + var colNo = frame.GetFileColumnNumber(); if (lineNo != 0) { - frame.ColumnNumber = colNo; + sentryFrame.ColumnNumber = colNo; + } + + var sourceInfo = DetailsProvider.GetStackFrameSourceCodeInfo( + sentryFrame.Function, + sentryFrame.AbsolutePath, + sentryFrame.LineNumber ?? 0); + + var contextLines = sourceInfo.ContextCode.ToList(); + if (contextLines.Count > 0) + { + sentryFrame.ContextLine = string.Join("\n", contextLines); + sentryFrame.PreContext = sourceInfo.PreContextCode.ToList(); + sentryFrame.PostContext = sourceInfo.PostContextCode.ToList(); } // TODO: Consider Ben.Demystifier (not on netcoreapp2.1+ which has by default) - DemangleAsyncFunctionName(frame); - DemangleAnonymousFunction(frame); + DemangleAsyncFunctionName(sentryFrame); + DemangleAnonymousFunction(sentryFrame); - return frame; + // TODO: Consider Ben.Demystifier (not on netcoreapp2.1+ which has by default) + return sentryFrame; // TODO: make this extensible bool IsSystemModuleName(string moduleName) diff --git a/src/Sentry/Sentry.csproj b/src/Sentry/Sentry.csproj index 18495a190a..89e03eb57c 100644 --- a/src/Sentry/Sentry.csproj +++ b/src/Sentry/Sentry.csproj @@ -11,4 +11,14 @@ + + + + + + + + + +