Skip to content

Commit 07c0332

Browse files
Update guidance for Windows Forms apps (#5515)
1 parent d64708a commit 07c0332

File tree

3 files changed

+164
-4
lines changed

3 files changed

+164
-4
lines changed

src/platforms/dotnet/guides/winforms/index.mdx

Lines changed: 164 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,178 @@ redirect_from:
55
- /platforms/dotnet/winforms/
66
---
77

8-
In addition to initializing the SDK with [`SentrySdk.Init`](/platforms/dotnet/), you must also configure the WinForms error handler.
9-
108
Sentry's .NET SDK works with WinForms applications through the [Sentry NuGet package](https://www.nuget.org/packages/Sentry). It works with WinForms apps running on .NET Framework 4.6.1, .NET Core 3.0, or higher.
119

1210
## Configuration
1311

14-
The SDK automatically handles AppDomain.UnhandledException. On Windows Forms, for production apps (when no debugger is attached), the small Window pops up when an unhandled error occurs. To get the exception to rethrow the error so it is captured by Sentry, also configure:
12+
After adding the NuGet package, the SDK should be initialized in the program's main entry point, before launching any forms or performing signifanct work. The options for initialization vary by language.
13+
14+
### C# Initialization
15+
16+
A C# Windows Forms application will have a `Program.cs` file. You should initialize Sentry in this file.
17+
18+
You should also set `Application.SetUnhandledExceptionMode(UnhandledExceptionMode.ThrowException)` so Sentry can capture unhandled exceptions.
19+
20+
A complete example of the recommended startup is as follows:
1521

1622
```csharp
23+
using System;
1724
using System.Windows.Forms;
25+
using Sentry;
26+
27+
namespace WindowsFormsApp1
28+
{
29+
static class Program
30+
{
31+
/// <summary>
32+
/// The main entry point for the application.
33+
/// </summary>
34+
[STAThread]
35+
static void Main()
36+
{
37+
38+
// These WinForms options are usually set by default
39+
Application.EnableVisualStyles();
40+
Application.SetCompatibleTextRenderingDefault(false);
41+
42+
// Add this so Sentry can catch unhandled exceptions
43+
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.ThrowException);
44+
45+
// Configure the options for Sentry
46+
var sentryOptions = new SentryOptions
47+
{
48+
Dsn = "___PUBLIC_DSN___",
49+
// any other options you need go here
50+
};
51+
52+
// Initialize Sentry and run the main form of the application
53+
using (SentrySdk.Init(sentryOptions))
54+
{
55+
Application.Run(new YourMainApplicationForm());
56+
}
57+
}
58+
}
59+
}
60+
```
61+
62+
### Visual Basic Initialization
63+
64+
Windows Forms applications written in VB have two different intialization options. Pick one of them (not both).
65+
66+
#### Option 1
67+
68+
You can initialize Sentry in a similar approach as shown above for C#.
69+
70+
First, modify the settings on the project properties page as follows:
71+
- Clear the "Enable application framework" checkbox.
72+
- Set the "Startup object" to "Sub Main".
73+
74+
![VB Project Properties](vbsettings1.png)
75+
76+
Then, add the following file as `Program.vb`:
77+
78+
```vb
79+
Imports Sentry
80+
81+
Module Program
82+
83+
''' <summary>
84+
''' The main entry point for the application.
85+
''' </summary>
86+
<STAThread>
87+
Sub Main()
88+
89+
' These WinForms options are usually set by default
90+
Application.EnableVisualStyles()
91+
Application.SetCompatibleTextRenderingDefault(False)
92+
93+
' Add this so Sentry can catch unhandled exceptions
94+
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.ThrowException)
95+
96+
' Configure the options for Sentry
97+
Dim sentryOptions = New SentryOptions With
98+
{
99+
.Dsn = "___PUBLIC_DSN___",
100+
' any other options you need go here
101+
}
102+
103+
' Initialize Sentry and run the main form of the application
104+
Using SentrySdk.Init(sentryOptions)
105+
Application.Run(New YourMainApplicationForm)
106+
End Using
107+
End Sub
108+
109+
End Module
110+
```
111+
112+
#### Option 2
113+
114+
If you prefer to keep the WinForms "Application Framework" feature enabled, then you can initialize Sentry in the "Application Events".
115+
116+
First, ensure the settings on the project properties page are as follows:
117+
- The "Enable application framework" checkbox should be checked.
118+
- The "Startup object" should be set to the name of your main form.
119+
120+
![VB Project Properties](vbsettings2.png)
121+
122+
Next, you will need to modify the `ApplicationEvents.vb` file.
123+
- If you are using .NET Framework, clicking the "View Application Events" button will create and open the `ApplicationEvents.vb` file.
124+
- If you are using .NET Core, you will need to add the file manually.
125+
- If you are using .NET 5 or higher, the file should already be present. If not, you can add it manually.
126+
127+
Once you have the file created:
128+
129+
- Initialize Sentry in the `Startup` event, and retain the result.
130+
- Dispose Sentry in the `Shutdown` event, using the result from initialization.
131+
- Use the `UnhandledException` event to send unhandled exceptions to sentry.
132+
133+
See the following complete example:
134+
135+
```vb
136+
Imports Microsoft.VisualBasic.ApplicationServices
137+
Imports Sentry
138+
Imports Sentry.Protocol
139+
140+
Namespace My
141+
Partial Friend Class MyApplication
142+
143+
Private _sentry As IDisposable
144+
145+
Private Sub MyApplication_Startup(sender As Object, e As StartupEventArgs) Handles Me.Startup
146+
147+
' Configure the options for Sentry
148+
Dim sentryOptions = New SentryOptions With
149+
{
150+
.Dsn = "___PUBLIC_DSN___",
151+
' any other options you need go here
152+
}
153+
154+
' Initialize Sentry
155+
_sentry = SentrySdk.Init(sentryOptions)
156+
157+
End Sub
158+
159+
Private Sub MyApplication_Shutdown(sender As Object, e As EventArgs) Handles Me.Shutdown
160+
161+
' Dispose the Sentry SDK to ensure events are flushed and sent to Sentry
162+
_sentry.Dispose()
163+
164+
End Sub
165+
166+
Private Sub MyApplication_UnhandledException(sender As Object, e As UnhandledExceptionEventArgs) Handles Me.UnhandledException
167+
168+
' Set some additional data on the exception for Sentry to recognize this exception as unhandled
169+
Dim ex = e.Exception
170+
ex.Data(Mechanism.HandledKey) = False
171+
ex.Data(Mechanism.MechanismKey) = "WindowsFormsApplicationBase.UnhandledException"
172+
173+
' Capture the exception with Sentry
174+
SentrySdk.CaptureException(ex)
175+
176+
End Sub
18177

19-
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.ThrowException);
178+
End Class
179+
End Namespace
20180
```
21181

22182
### Resources
Loading
Loading

0 commit comments

Comments
 (0)