I have an ASP.NET MVC (4.6.1) website that we are trying to set up monitoring on with the Sentry service.
According to the setup documentation, it just says to initialize Sentry as early as possible, but the structure of their example gives me cause to doubt that there isnt more to it. In my Gloabl.asax.cs file, I am calling a Custom model class that includes the Sentry Initialization. Here is a copy of the class:
public class SentryModel
{
public static void Configure()
{
var environment = ConfigurationManager.AppSettings["Environment"];
//escape the method if we are in a development environment
if (environment.Equals("development", StringComparison.CurrentCultureIgnoreCase))
return;
Assembly web = Assembly.GetExecutingAssembly();
AssemblyName webName = web.GetName();
string myVersion = webName.Version.ToString();
string dsn_data = ConfigurationManager.ConnectionStrings["Sentry"].ConnectionString;
using (SentrySdk.Init(o =>
{
o.Dsn = new Dsn(dsn_data);
o.MaxBreadcrumbs = 50;
o.Debug = true;
o.Environment = environment;
o.Release = myVersion;
o.AttachStacktrace = true;
}))
{
// app code here
}
}
}
My concern here is that we should really have something where the "//app code here" goes, but there is no guidance on what that is specifically. We obviously want sentry to monitor all error and events that happen within the app service. I have seen a few examples where an exception was explicitly sent to Sentry but nothing about the proper way to initialize the service and handle passive capturing.
Thanks
The example you used, where the comment application code here, can't be used with ASP.NET classic since the real start of the app is managed by IIS.
SentrySdk.Init
returns an object that implements IDisposable
and is used to gracefully shutdown the SDK. This is needed to ensure the internal event queue is flushed out before the app shuts down. This way you don't lose any events.
In your current setup, at the end of the Configure
method, the SDK will be disabled because you've wrapped it in a using
block. So it'll be initialized and immediately closed down.
What you need to do is to call Init
during startup and dispose the object it returns on application shutdown. Besides that, add SentrySdk.CaptureException
in the global.asax
's Application_Error
event handler.
Sentry has an example on GitHub on how to use the SDK with 'classic' ASP.NET and global.asax
here but the important parts are as follows:
protected void Application_Start()
{
// Set up the sentry SDK
_sentry = SentrySdk.Init(o =>
{
o.Dsn = new Dsn(ConfigurationManager.AppSettings["SentryDsn"]);
});
}
protected void Application_Error()
{
var exception = Server.GetLastError();
// Capture unhandled exceptions
SentrySdk.CaptureException(exception);
}
protected void Application_End()
{
// Close the Sentry SDK (flushes queued events to Sentry)
_sentry?.Dispose();
}