getsentry / sentry-dotnet

Sentry SDK for .NET
https://docs.sentry.io/platforms/dotnet
MIT License
590 stars 206 forks source link

Add SentrySdk.Init() only once for the entire application lifetime #446

Closed jeffvannijlen closed 3 years ago

jeffvannijlen commented 4 years ago

Please mark the type framework used:

Please mark the type of the runtime used:

Please mark the NuGet packages used:

Hi,

We are trying to create a .netCore 2.2 Console app to Track Exceptions. However I was wondering if it's possible to do this without adding SentrySdk.Init() around every single statement.

Are there any issues doing the init once for the entire application lifetime? Is it safe if it will never be disposed?

Another question, is SentrySdk.Init(dsn); the only way to initialize this? I already tried using SentrySdk.Init(options => ctx.Configuration.GetSection("Sentry").Bind(options)); but this doesn't seem to work.

Or is there a better way to do this?

Thanks in advance!

Best regards

Jeff Van Nijlen

bruno-garcia commented 4 years ago

Sentry's SDK is designed to be initialized only once for the lifetime of the app.

You can hold the reference to the disposable that Init returns to call only when the app starts,

On a console app, the easiest way is to:

Main() {
using (SentrySdk.Init(..)) {
// App code here
}
}

You need to dispose it because that makes sure the SDK attempts to flush out queued events. Otherwise the app might close while your errors are still queued up for submission

If you're using Microsoft.Extensions.Logging you can follow: https://github.com/getsentry/sentry-dotnet/blob/master/samples/Sentry.Samples.ME.Logging/Program.cs#L12

There are more samples in that directory