0
votes

When I try to log the user session using the steps from this post: Send user context IDs to enable usage experiences in Azure Application Insights in webforms, the information is not available in Azure Application Insights

Repro Steps 1. Add Telemetry Inicializer

public class TelemetryInitializer : ITelemetryInitializer
{
    public void Initialize(ITelemetry telemetry)
    {
        var ctx = HttpContext.Current;

        // If telemetry initializer is called as part of request execution and not from some async thread
        if (ctx != null)
        {
            var requestTelemetry = ctx.GetRequestTelemetry();

            // Set the user and session ids from requestTelemetry.Context.User.Id, which is populated in Application_PostAcquireRequestState in Global.asax.cs.
            if (requestTelemetry != null && !string.IsNullOrEmpty(requestTelemetry.Context.User.Id) &&
                (string.IsNullOrEmpty(telemetry.Context.User.Id) || string.IsNullOrEmpty(telemetry.Context.Session.Id)))
            {
                // Set the user id on the Application Insights telemetry item.
                telemetry.Context.User.Id = requestTelemetry.Context.User.Id;

                // Set the session id on the Application Insights telemetry item.
                telemetry.Context.Session.Id = requestTelemetry.Context.User.Id;
            }
        }
    }
}
  1. Register the Initializer at Global.asax
TelemetryConfiguration.Active.TelemetryInitializers.Add(new ManageTelemetryInitializer());
  1. Fill the data at Application_PostAcquireRequestState
protected void Application_PostAcquireRequestState()
{
    var requestTelemetry = Context.GetRequestTelemetry();
    if (HttpContext.Current.Session != null && requestTelemetry != null && string.IsNullOrEmpty(requestTelemetry.Context.User.Id))
    {
        UserSession userSession = // GetMySession

        string userId = string.Empty;

        if (userSession != null)
        {
            userId = userSession.UserGuid.ToString("N");
        }

        requestTelemetry.Context.User.Id = userId;
        requestTelemetry.Context.Session.Id = Session.SessionID;
    }
}

Actual Behavior Session data is not available in Azure Application Insights

enter image description here

Expected Behavior Having the correct user session in Azure Application Insights

UPDATE:

Here's the data which I can see on the output window when I debug. Everything looks fine. I omitted some fields because of privacy.

{
    "name": "Microsoft.ApplicationInsights.Dev.fc9e8309cdd74395bf57f81d63d915d9.Message",
    "time": "2019-10-18T10:13:21.0548760Z",
    "sampleRate": 33.3333333333333,
    "iKey": "INSTRUMENTATION_KEY",
    "tags": {
        "ai.internal.sdkVersion": "sd:2.4.1-442",
        "ai.session.id": "oaluibbilrmunjxc3i1zovld",
        "ai.operation.id": "OPERATION_ID",
        "ai.location.ip": "::1",
        "ai.cloud.roleInstance": "MACHINE_NAME",
        "ai.user.id": "cef5121344374f25a325a1079473c51a",
        "ai.operation.name": "OEPRATION",
        "ai.operation.parentId": "PARENT_ID"
    },
    "data": {
        "baseType": "MessageData",
        "baseData": {
            "ver": 2,
            "message": "MESSAGE",
            "severityLevel": "Verbose",
            "properties": {
                "DeveloperMode": "true"
            }
        }
    }
} 
1
During debugging session do you have userId and Session.SessionID set when the Application_PostAcquireRequestState is called ?Pietro
@Pietro everything is set locallyFals
Do you see the telemetry in Application Insights. I mean everything else works as expected?Pietro
Everything works as expected, the only thing that I can't see is the Session. Is not sending the proper data. As I mentioned I fallowed microsoft guid to implement it, and even though is not working properly, the user sessions are not logged correctly.Fals
I tried to reproduce your problem on a clean web project. The only issue I did have was the Context.Session was null. My understanding is that this is working on your side. After enabling the session, I do see session_Id and user_Id in Azure Application Insights. I am using the implementation you provided in your question as a link to ms page.Pietro

1 Answers

0
votes

Mystery was solved. A combination of filters in Azure Application Insights was misleading the team from NOC. They were seeing no data because of it.