我有一个 ASP.NET MVC (4.6.1) 网站,我们正在尝试使用 Sentry 服务对其进行监控。
根据设置文档,它只是说要尽早初始化 Sentry,但是他们示例的结构让我有理由怀疑没有更多的东西。在我的 Gloabl.asax.cs 文件中,我正在调用一个包含 Sentry 初始化的自定义模型类。这是类(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
}
}
}
我在这里担心的是,我们真的应该在“//app code here”所在的地方有一些东西,但没有关于具体是什么的指导。我们显然希望 Sentry 监控应用服务中发生的所有错误和事件。我已经看到一些示例,其中异常被显式发送到 Sentry,但没有关于初始化服务和处理被动捕获的正确方法。
谢谢
最佳答案
您使用的示例,其中注释此处的应用程序代码,不能与 ASP.NET classic 一起使用,因为应用程序的真正启动由 IIS 管理。
SentrySdk.Init
返回一个实现了 IDisposable
的对象,用于正常关闭 SDK。这是确保在应用程序关闭之前清除内部事件队列所必需的。这样您就不会丢失任何事件。
在您当前的设置中,在 Configure
方法结束时,SDK 将被禁用,因为您已将其包装在 using
block 中。所以它会被初始化并立即关闭。
您需要做的是在启动期间调用 Init
并在应用程序关闭时处理它返回的对象。除此之外,在 global.asax
的 Application_Error
事件处理程序中添加 SentrySdk.CaptureException
。
Sentry 有一个 example on GitHub on how to use the SDK with 'classic' ASP.NET and global.asax
here但重要的部分如下:
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();
}
关于.net - ASP.NET Sentry 配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56085476/