2
votes

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
Have you tried use these methods like TrackEvent() / TrackRequest()? and then check if the custom user_id / session_id are set correctly?Ivan Yang
I'm not trying to put in code to track this stuff. The SDK tracks these things for me. The question is, why doesn't the SDK pick up this data automatically?Matt M
Pretty sure this would populate Auth Id in App Insights, not User Id. Seems like User Id is uniquely tied to a particular client/cookie, but its generated by Azure so not really helpful. Your code should set Auth Id. Can you check?Don Cheadle

1 Answers

1
votes

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