无法将用户上下文 ID 发送到 Azure Application Insights

Not able to send user context IDs to Azure Application Insights

当我尝试使用此 post 中的步骤记录用户会话时:Send user context IDs to enable usage experiences in Azure Application Insights 在网络表单中,该信息在 Azure Application Insights 中不可用

复制步骤 1. 添加 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. 在 Global.asax
  2. 处注册初始化器
TelemetryConfiguration.Active.TelemetryInitializers.Add(new ManageTelemetryInitializer());
  1. 在Application_PostAcquireRequestState
  2. 填写数据
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;
    }
}

实际行为 会话数据在 Azure Application Insights 中不可用

预期行为 在 Azure Application Insights 中拥有正确的用户会话

更新:

这是我在调试时可以在输出 window 上看到的数据。一切看起来都很好。由于隐私原因,我省略了一些字段。

{
    "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"
            }
        }
    }
} 

谜团解开了。 Azure Application Insights 中的过滤器组合误导了来自 NOC 的团队。因此他们看不到任何数据。