Application Insights - Tracking user and session across schemas

这一生的挚爱 提交于 2020-01-15 09:31:49

问题


Following https://docs.microsoft.com/en-us/azure/application-insights/app-insights-usage-send-user-context, I thought it would be easy to get cross-schema tracking of a user. However, I'm finding the absolute opposite.

I created the telemetry initializer (which the document has bugs in it hardcore):

public void Initialize(ITelemetry telemetry)
    {
        if (HttpContext.Current?.Session == null)
            return;

        if (HttpContext.Current.Session["UserId"] == null)
        {
            HttpContext.Current.Session["UserId"] = Guid.NewGuid().ToString();
        }

        telemetry.Context.User.Id = (string)HttpContext.Current.Session["UserId"];
        telemetry.Context.Session.Id = HttpContext.Current.Session.SessionID;

        var authUser = _sessionManager.GetAuthenticatedUser<UserDetails>();
        if (authUser != null)
        {
            telemetry.Context.User.AuthenticatedUserId = authUser.UserId;
        }
    }

Then I went and added it to App Insights

TelemetryConfiguration.Active.TelemetryInitializers.Add(new UserTrackingTelemetryInitializer());

I then played with my site, expecting this stuff to start showing up. It did not. I continued to get random strings for user_Id and session_Id (things like NVhLF and what not). So, I thought, okay, maybe it's logging before I update those values? I went and inserted my initializer first:

TelemetryConfiguration.Active.TelemetryInitializers.Insert(0, new UserTrackingTelemetryInitializer());

Same thing. So I started to look at schemas I don't usually look at. Nothing. So I pulled up traces and I found it. Finally, there is where my data is going. But the other schemas don't have the updated values, so what use is this? While traces is showing the expected values for user_Id and session_Id, the others continue to show garbage. Am I doing something wrong?


回答1:


The document you followed does not work indeed, a feedback has been submitted here.

Just for your reference, the way I can find to set these values is that use such TrackEvent() / TrackRequest() or other Trackxxx() methods after implemented your own telemetry initializer



来源:https://stackoverflow.com/questions/52672214/application-insights-tracking-user-and-session-across-schemas

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!