【发布时间】:2018-10-10 10:06:47
【问题描述】:
在我的一项任务中,必须在应用程序洞察力中获取 Web 服务方法名称。
在我的类文件中编写以下代码。
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