如何从requestTelemetry获取会话ID?

时间:2018-09-12 19:04:42

标签: c# azure-application-insights

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MyLibrary.Web.Context;
using Microsoft.ApplicationInsights.Channel;
using Microsoft.ApplicationInsights.Extensibility;


namespace ACME.Foundation.Analytics
{
    public class ApplicationInsightsSessionInitializer : ITelemetryInitializer
    {
        public void Initialize(ITelemetry telemetry)
        {
            var requestTelemetry = telemetry as RequestTelemetry;
            if (requestTelemetry == null) return;

            if (HttpContext.Current.Session.SessionID != null)
            {
                telemetry.Context.Session.Id = HttpContext.Current.Session.SessionID;
            }
        }
    }
}

在ApplicationInsights.config

<Add Type="ACME.Foundation.Analytics.ApplicationInsightsSessionInitializer, ACME.Foundation.Analytics" />
<Add Type="Microsoft.ApplicationInsights.StatusMonitor.SdkSourceTelemetryInitializer, Microsoft.AI.StatusMonitor" />

如何从requestTelemetry获取会话ID?我的分析遥测文件夹中有此代码段。似乎itemType请求没有会话ID。每次出现请求时,会话为空。

我想将其与正确的sessionID匹配,并且无法在应用数据分析sessionID中覆盖它

我在这里遵循以下示例:https://docs.microsoft.com/en-us/azure/application-insights/app-insights-usage-send-user-context

并根据位于页面中间的演示程序提供了我的解决方案,但似乎对我不起作用。

任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:0)

根据@Dmitry的说明,该示例不正确。

还有另一种设置会话ID的方法,您可以参考以下link

private static TelemetryClient GetTelemetryClient()
{
var telemetryClient = new TelemetryClient();
telemetryClient.InstrumentationKey = "<your actual insight instrumentkey>";
telemetryClient.Context.Session.Id = "124556";
//update 1-Custom properties- Start
telemetry.Context.Properties["tags"] = "PROD";
telemetry.Context.Properties["userCorelateId"]="1234"; 
//update 1-Custom properties- Ends                
return telemetryClient;
}