简体   繁体   中英

How to integrate server side Application Insights telemetry into my Umbraco / Articulate web site ideally?

I have created an Umbraco web site by doing the following

  1. Open Visual Studio
  2. Create a new empty MVC project
  3. Added the current Umbraco nuget package
  4. Hosted the project in Azure
  5. Successfully executed Umbraco installer
  6. Installed the current Articulate package

Now I would like to integrate Application Insights . The client side part is very easy, I just need to add some JavaScript code into the master view.

For the server side part I need to add this code:

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());
   }
}

My question in detail is the following:

How do I integrate this code ideally touching as less as possible in the Umbraco core to make updates in the future as easy as possible? What is the best approach to integrate the code?

Do I need to touch the Umbraco core or is it possible just to change the Articulate code? Or even better: Can I create my own Umbraco package which can add Application Insights feaature to my Umbraco instance (maybe both client and server side part)?

You shouldn't need to touch the Umbraco Core for this. You could quite easily do it as your own custom code, which you could then turn into a plugin. The attribute you could just add as a class, and to hook it in, rather than the FilterConfig event, you can use the Umbraco Startup Handler: https://our.umbraco.org/documentation/reference/events/application-startup

You could register your filter in the ApplicationStarted event handler of the startup handler, that should work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM