You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/platforms/dotnet/common/troubleshooting.mdx
+52-1Lines changed: 52 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -9,6 +9,57 @@ description: "Learn more about how to troubleshoot common issues with the .NET S
9
9
If you're using a proxy server that relies on `X-Forwarded-For`, you might need to [configure ASP.NET Core
10
10
so that it's aware of it](https://docs.microsoft.com/aspnet/core/host-and-deploy/proxy-load-balancer).
11
11
12
-
See [this GitHub issue] (https://github.com/getsentry/sentry-dotnet/issues/258) for more context.
12
+
See [this GitHub issue](https://github.com/getsentry/sentry-dotnet/issues/258) for more context.
13
+
14
+
## Unhandled exceptions are not captured when using an async `Main` method
15
+
16
+
Starting with C# 7.1, a program's [`Main` method](https://docs.microsoft.com/dotnet/csharp/fundamentals/program-structure/main-command-line) can be declared either synchronously or asynchronously. However, the asynchronous approach is not fully compatible with the Sentry SDK. If your application uses an async `Main` method, the Sentry SDK will be disposed before an unhandled exception can be captured.
17
+
18
+
In other words, this is insufficient:
19
+
20
+
```csharp
21
+
staticasyncTaskMain()
22
+
{
23
+
usingvar_=SentrySdk.Init(o=> { /* your options */ });
24
+
awaitDoSomethingAsync(); // an unhandled exception here will not be captured by Sentry
25
+
}
26
+
```
27
+
28
+
There are two different ways to work around this issue:
29
+
30
+
- You can avoid unhandled exceptions by wrapping your code in a try/catch block:
31
+
```csharp
32
+
staticasyncTaskMain()
33
+
{
34
+
usingvar_=SentrySdk.Init(o=> { /* your options */ });
35
+
try
36
+
{
37
+
awaitDoSomethingAsync();
38
+
}
39
+
catch (Exceptionex)
40
+
{
41
+
SentrySdk.CaptureException(ex);
42
+
}
43
+
}
44
+
```
45
+
46
+
- You can use a synchronous `Main` method to ensure the Sentry SDK isn't disposed prematurely:
47
+
48
+
```csharp
49
+
staticvoidMain()
50
+
{
51
+
usingvar_=SentrySdk.Init(o=> { /* your options */ });
52
+
MainAsync().GetAwaiter().GetResult();
53
+
}
54
+
55
+
staticasyncTaskMainAsync()
56
+
{
57
+
awaitDoSomethingAsync();
58
+
}
59
+
```
60
+
61
+
Note that with this approach, it is not advised to use `.Wait()` or `.Result`, as that would wrap unhandled exceptions in an `AggregateException`.
62
+
63
+
See [this GitHub issue](https://github.com/getsentry/sentry-dotnet/issues/321) for more context.
0 commit comments