【问题标题】:Application Insights – Capture ASMX WebMethod Names code not getting triggeredApplication Insights – 捕获未触发的 ASMX WebMethod 名称代码
【发布时间】:2018-10-10 10:06:47
【问题描述】:

在我的一项任务中,必须在应用程序洞察力中获取 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 没有被触发的原因。

在所有情况下,依赖调用都会登录应用洞察,但我的更改不会到来。

帮我解决这个问题。

【问题讨论】:

  • 程序集名称是什么?您可以尝试在代码中注册它,例如在 Global.aspx.cs 文件中 -> Application_Start() 方法:TelemetryConfiguration.Active.TelemetryInitializers.Add(new ApplicationInsightsMethodLogInitializer());
  • 您在哪里安装了应用程序洞察力?从视觉工作室菜单选项或通过 nuget?有一个类似的issue,你可以看看。

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


【解决方案1】:

对于 asp.net mvc 项目,请通过右键单击项目名称 -> 配置应用程序洞察力来安装应用程序洞察力:

请确保它在 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 中注册或在代码中对其进行了测试,两者都可以正常工作。

我的初始化程序示例:

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

执行后,你可以看到初始化类在调试模式下被命中:

并且在初始化类被调用后添加了新属性:

【讨论】:

  • 很高兴它对你有用。能否请您将其标记为答案,然后对其他人有帮助。
  • 我的 Web 服务从不同的服务器调用,所以我想在 TelemetryInitializer 中获取 Web 服务的方法名称。你能帮我吗
  • 是的,但是最好提交一个新帖子,这样如果超出我的范围,我可以帮助升级它。
  • 感谢 Ivan,创建了一个新帖子。 link
  • 你能帮我把这个标记为答案吗?谢谢。我现在正在处理新问题。
猜你喜欢
  • 2021-01-14
  • 2021-08-02
  • 1970-01-01
  • 1970-01-01
  • 2022-01-11
  • 2023-03-04
  • 1970-01-01
  • 1970-01-01
  • 2021-11-02
相关资源
最近更新 更多