Application Insights –未触发捕获ASMX WebMethod Names代码

时间:2018-10-05 14:02:37

标签: asp.net-mvc azure-application-insights

在我的一项任务中,必须在应用程序洞察中获取Web服务方法的名称。

我在下面的链接中引用了
  https://unhandled.wordpress.com/2018/02/11/application-insights-capture-asmx-webmethod-names-invoked/

在我的班级文件中的下面代码中编写。

public class ApplicationInsightsMethodLogInitializer: ITelemetryInitializer
    {
        public void Initialize(ITelemetry telemetry)
        {
            var requestTelemetry = telemetry as RequestTelemetry;
            string soapActionMethod = null;
            string requestMethodName = null;
            string webServiceMethod = null;
            var logger = Sitecore.DependencyInjection.ServiceLocator.ServiceProvider.GetService<ILogger>();
            logger.Info("HI Pradeep Here");
            // Is this a TrackRequest() ?
            if (requestTelemetry == null) return;
            requestMethodName = System.Web.HttpContext.Current.Request.Params["op"]; // Item("HTTP_SOAPACTION");
            if (requestMethodName == "" || requestMethodName == null)
            {
                if (System.Web.HttpContext.Current.Request.PathInfo != null)
                {
                    requestMethodName = System.Web.HttpContext.Current.Request.PathInfo;
                }
                if (requestMethodName != "" && requestMethodName != null)
                {
                    logger.Info("Got It..");
                    requestMethodName = requestMethodName.Replace("/", "");
                    // If we set the Success property, the SDK won't change it:
                    requestTelemetry.Success = true;
                    // Allow us to filter these requests in the portal:
                    requestTelemetry.Properties["WebMethodName"] = requestMethodName;
                    webServiceMethod = requestMethodName;
                }
            }
            if (webServiceMethod != null)
            {
                requestTelemetry.Context.Operation.Name = requestTelemetry.Context.Operation.Name.Replace("/" + webServiceMethod, "") + "/" + webServiceMethod;
            }
        }

然后在ApplicationInsights.config中注册它:

 <Add Type="Microsoft.ApplicationInsights.Web.ClientIpHeaderTelemetryInitializer, Microsoft.AI.Web"/>
    <Add Type="Microsoft.ApplicationInsights.Web.OperationNameTelemetryInitializer, Microsoft.AI.Web"/>
    <Add Type="GCC.Foundation.Analytics.ApplicationInsightsMethodLogInitializer, GCC.Foundation.Analytics" />

在运行代码时,在类文件中未触发代码且调试器也未命中。

结果应类似于webserviceName / MethodName。

我试图从运行中的显式调用中调用此代码,因为调用了类方法。

我错过了什么吗,这就是为什么未触发ApplicationInsightsMethodLogInitializer的原因。

在所有情况下,依赖项调用都已登录到应用程序洞察力中,但是我的更改没有来临。

在这方面帮助我。

1 个答案:

答案 0 :(得分:0)

对于asp.net mvc项目,请通过右键单击项目名称->配置应用程序见解来安装应用程序见解: enter image description here

并且请确保它在ApplicationInsights.config中正确注册,并且应遵循以下规则:

<Add Type "Fully qualified type name, assembly name"/>

或者您也可以在代码中实例化初始化程序,例如在Global.aspx.cs中:

    protected void Application_Start()
    {

     TelemetryConfiguration.Active.TelemetryInitializers.Add(new MyTelemetryInitializer());
    }

有关详细信息,请参阅here

我在我的身边进行了测试,要么在ApplicationInsights.config中注册,要么在代码中都正常工作。

我的Initializer示例:

namespace WebMVCStandard
{
    public class MyTelemetryInitializer:ITelemetryInitializer
    {
        public void Initialize(ITelemetry telemetry)
        {
            if (!telemetry.Context.Properties.ContainsKey("Application123"))
            {
                telemetry.Context.Properties.Add("Application123", "todoxxxx");
            }
        }
    }
}

在ApplicationInsights.config中注册:

<TelemetryInitializers>
  <Add Type ="WebMVCStandard.MyTelemetryInitializer,WebMVCStandard"/>
</TelemetryInitializers>

或者在Global.aspx.cs中的代码中:

namespace WebMVCStandard
{
    public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
            TelemetryConfiguration.Active.TelemetryInitializers.Add(new MyTelemetryInitializer());
        }
    }
}

执行后,可以看到在调试模式下命中了初始化器类: enter image description here

在调用初始化器类之后,还会添加新属性: enter image description here