ASP.NET 哨兵配置

Pet*_*nge 2 .net asp.net-mvc global-asax sentry

我有一个 ASP.NET MVC (4.6.1) 网站,我们正在尝试使用 Sentry 服务对其进行监控。

根据安装文档,它只是说尽早初始化 Sentry,但他们示例的结构让我有理由怀疑是否没有更多内容。在我的 Gloabl.asax.cs 文件中,我调用包含 Sentry 初始化的自定义模型类。这是该课程的副本:

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
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

我在这里担心的是,我们确实应该有一些“//app code here”所在的地方,但没有具体说明它是什么的指导。我们显然希望哨兵监视应用程序服务中发生的所有错误和事件。我见过一些示例,其中将异常显式发送到 Sentry,但没有介绍初始化服务和处理被动捕获的正确方法。

谢谢

Bru*_*cia 6

您使用的示例(此处的注释应用程序代码)不能与 ASP.NET classic 一起使用,因为应用程序的真正启动是由 IIS 管理的。

SentrySdk.Init返回一个实现IDisposable并用于正常关闭 SDK 的对象。这是为了确保在应用程序关闭之前清除内部事件队列所必需的。这样您就不会丢失任何事件。

在您当前的设置中,在方法结束时Configure,SDK 将被禁用,因为您已将其包装在using块中。因此它将被初始化并立即关闭。

您需要做的是在启动期间调用Init并在应用程序关闭时处理它返回的对象。除此之外,添加SentrySdk.CaptureExceptionglobal.asax事件Application_Error处理程序。

Sentry在 GitHub 上有一个关于如何将 SDK 与“经典”ASP.NET 一起使用的示例,global.asax此处但重要部分如下:


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();
}
Run Code Online (Sandbox Code Playgroud)