【问题标题】:ASP.NET Sentry ConfigurationASP.NET 哨兵配置
【发布时间】:2019-05-10 22:19:03
【问题描述】:

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

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

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,但对初始化服务和处理被动捕获的正确方法一无所知。

谢谢

【问题讨论】:

    标签: .net asp.net-mvc global-asax sentry


    【解决方案1】:

    您使用的示例,其中注释 application code here 不能与 ASP.NET classic 一起使用,因为应用程序的真正启动由 IIS 管理。

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

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

    您需要做的是在启动期间调用Init 并在应用程序关闭时处理它返回的对象。除此之外,在global.asaxApplication_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();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-09
      • 2017-05-25
      • 1970-01-01
      • 1970-01-01
      • 2017-02-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多