Exception Thrown in a Static Constructor Is Not Being Sent To Sentry
See original GitHub issueEnvironment
How do you use Sentry? Sentry SaaS
Which SDK and version? sentry.dotnet@v3.8.1
Steps to Reproduce
Exception thrown in a static constructor doesn’t get enqueued.
For example, we have a class with a static constructor that throws exception
public class Address
{
static Address()
{
var cultures = CultureInfo.GetCultures(CultureTypes.SpecificCultures)
.Select(x => new RegionInfo("qwerty")) //this will throw
.Where(x => x.DisplayName != "World").ToList();
}
}
when exception is handled like this:
try
{
var a = new Address();
}
catch (Exception ex)
{
Sentry.CaptureException(ex);
}
where Sentry
is a wrapper class like this:
public class Sentry
{
public static readonly SentryClient Instance;
static Sentry()
{
Instance = new SentryClient(new SentryOptions {
Environment = "development"
Debug = true,
DiagnosticLevel = SentryLevel.Debug,
DiagnosticLogger = new TraceDiagnosticLogger(SentryLevel.Debug),
ServerName = System.Environment.MachineName,
Dsn = ($"my-dsn")
});
}
public static void CaptureException(Exception ex)
{
var sentryEvent = new SentryEvent(ex);
Instance.CaptureEvent(sentryEvent);
}
}
in diagnostics mode, here’s the exception I see when CaptureEvent
is called:
Info: Capturing event.
Debug: Running processor on exception: The type initializer for 'testApp.Address' threw an exception.
The thread 0x20d0 has exited with code 0 (0x0).
Debug: Creating SentryStackTrace. isCurrentStackTrace: False.
The thread 0x1cdc has exited with code 0 (0x0).
'w3wp.exe' (CLR v4.0.30319: /LM/W3SVC/2/ROOT-13-132836386776909184): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Runtime.InteropServices.RuntimeInformation\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Runtime.InteropServices.RuntimeInformation.dll'.
The thread 0x1f48 has exited with code 0 (0x0).
The thread 0x2364 has exited with code 0 (0x0).
Exception thrown: 'System.Reflection.TargetInvocationException' in mscorlib.dll
The thread 0x2a8 has exited with code 0 (0x0).
Error: An error occurred when capturing the event a7b03ff9ef0c4f1187f6867df9ee8f19.
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.TypeInitializationException: The type initializer for 'testApp.Address' threw an exception. ---> System.ArgumentException: Culture name 'qwerty' is not supported.
Parameter name: name
at System.Globalization.RegionInfo..ctor(String name)
at testApp.Address.<>c.<.cctor>b__8_0(CultureInfo x) in \\Mac\Home\dev\testApp\Address.cs:line 43
at System.Linq.Enumerable.WhereSelectArrayIterator`2.MoveNext()
at System.Linq.Enumerable.WhereEnumerableIterator`1.MoveNext()
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at testApp.Address..cctor() in \\Mac\Home\dev\testApp\Address.cs:line 42
--- End of inner exception stack trace ---
--- End of inner exception stack trace ---
at System.RuntimeFieldHandle.GetValue(RtFieldInfo field, Object instance, RuntimeType fieldType, RuntimeType declaringType, Boolean& domainInitialized)
at System.Reflection.RtFieldInfo.UnsafeGetValue(Object obj)
at System.Reflection.RtFieldInfo.GetValue(Object obj)
at System.Diagnostics.EnhancedStackTrace.GetMethodDisplayString(MethodBase originMethod)
at System.Diagnostics.EnhancedStackTrace.GetFrames(StackTrace stackTrace)
at Sentry.Extensibility.SentryStackTraceFactory.<CreateFrames>d__5.MoveNext()
at System.Linq.Buffer`1..ctor(IEnumerable`1 source)
at System.Linq.Enumerable.<ReverseIterator>d__75`1.MoveNext()
at Sentry.Extensibility.SentryStackTraceFactory.Create(StackTrace stackTrace, Boolean isCurrentStackTrace)
at Sentry.Internal.MainExceptionProcessor.<CreateSentryException>d__9.MoveNext()
at Sentry.Internal.MainExceptionProcessor.<CreateSentryException>d__9.MoveNext()
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at Sentry.Internal.MainExceptionProcessor.Process(Exception exception, SentryEvent sentryEvent)
at Sentry.SentryClient.DoSendEvent(SentryEvent event, Scope scope)
at Sentry.SentryClient.CaptureEvent(SentryEvent event, Scope scope)
Expected Result
Event would be sent to Sentry.
Actual Result
CaptureEvent
is called but it hasn’t thrown exceptions and Sentry event wasn’t sent.
Issue Analytics
- State:
- Created 2 years ago
- Comments:6 (5 by maintainers)
Top Results From Across the Web
c# - Exception in static constructor
I think the takeaway from this is to not throw exceptions from within a Static Constructor, not to catch TypeInitialisationException everywhere, ...
Read more >How to Throw Exceptions in Java
Ensure that you have a constructor that sets the underlying throwable cause. Should your program catch a standard exception, your custom ...
Read more >#817 – What Happens When a Static Constructor Throws an ...
If any exception is thrown from within a static constructor, a TypeInitializationException will be thrown, with an InnerException set to the ...
Read more >Instrument incorrectly wrapping error causing sentry ...
The main problem is that the events that are sent to Sentry do not contain user data, although you set them in beforeSend...
Read more >Sentry produces incorrect data when async code is involved
When dealing with async code sentry fails to properly record data. Not only does it fail to record data, it also constantly adds...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@twinmind thanks for the awesome repro. this would not have been possible to solve without your help
fixing upstream in Ben.Demystifier: https://github.com/benaadams/Ben.Demystifier/pull/187