c# - 如何理想地将服务器端 Application Insights 遥测集成到我的 Umbraco/Articulate 网站中?

标签 c# asp.net-mvc azure umbraco azure-application-insights

我通过执行以下操作创建了一个 Umbraco 网站

  1. 打开 Visual Studio
  2. 创建一个新的空 MVC 项目
  3. 添加了 current Umbraco nuget package
  4. 在 Azure 中托管该项目
  5. 成功执行 Umbraco 安装程序
  6. 安装了current Articulate package

现在我想集成Application Insights 。客户端部分非常简单,我只需要在主视图中添加一些 JavaScript 代码即可。

对于服务器端部分,我需要添加以下代码:

using System;
using System.Web.Mvc;
using Microsoft.ApplicationInsights;

namespace MVC2App.Controllers
{
    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = true)] 
    public class AiHandleErrorAttribute : HandleErrorAttribute
    {
        public override void OnException(ExceptionContext filterContext)
        {
            if (filterContext != null && filterContext.HttpContext != null && filterContext.Exception != null)
            {
                //If customError is Off, then AI HTTPModule will report the exception
                if (filterContext.HttpContext.IsCustomErrorEnabled)
                {  
                    // Note: A single instance of telemetry client is sufficient to track multiple telemetry items.
                    var ai = new TelemetryClient();
                    ai.TrackException(filterContext.Exception);
                } 
            }
            base.OnException(filterContext);
        }
    }
}
// then register AiHandleErrorAttribute in FilterConfig:
public class FilterConfig
{
   public static void RegisterGlobalFilters(GlobalFilterCollection filters)
   {
     filters.Add(new AiHandleErrorAttribute());
   }
}

我的详细问题如下:

如何在 Umbraco 核心中尽可能少地集成此代码,以使将来的更新尽可能容易?集成代码的最佳方法是什么?

我需要接触 Umbraco 核心还是可以只更改 Articulate 代码?或者甚至更好:我可以创建自己的 Umbraco 包,它可以将 Application Insights 功能添加到我的 Umbraco 实例(可能是客户端和服务器端部分)?

最佳答案

您不需要为此接触 Umbraco 核心。您可以很容易地将其作为您自己的自定义代码来完成,然后您可以将其转换为插件。您可以将属性添加为类,并将其 Hook ,而不是 FilterConfig 事件,您可以使用 Umbraco 启动处理程序:https://our.umbraco.org/documentation/reference/events/application-startup

您可以在启动处理程序的 ApplicationStarted 事件处理程序中注册您的过滤器,这应该可以工作。

关于c# - 如何理想地将服务器端 Application Insights 遥测集成到我的 Umbraco/Articulate 网站中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36328337/

相关文章:

Azure 文本到语音 API 音频输出未播放

c# - 带有 CancellationToken 的 HttpClient 在 Xamarin 应用程序中不起作用

c# - 该属性必须是有效的实体类型,并且该属性应该具有非抽象 getter 和 setter。 Entity Framework

c# - 没有服务定位器的 IOC

c# - 问题级联下拉列表,生成的下拉列表未将选定值发布到服务器

asp.net - UserManager CheckPassword() 在.net core 3.1 中重新哈希密码,无法从 asp.net MVC 项目登录

python - Spark 池在 azure synapse Analytics 中启动需要时间

c# - 从 ObjectContext 转换为 DBContext

javascript - 在 jQuery UI 对话框中加载和编辑数据

azure - 有权在 Application Insights 中添加新的可用性测试,而无需访问资源组