Azure Application Insight 仅显示 1 个用户/会话

问题描述 投票:0回答:1

我使用 React 和 Node.js 作为前端,使用 C# 作为 API 后端。

我的应用程序服务的 API 后端的应用程序见解存在问题。我只看到一个会话和一个用户。我知道情况并非如此,因为我已经从两个不同的 IP 地址对其进行了测试,并记录了结果。

所有事件和错误似乎都记录在应用程序洞察中,我可以查看指标、事件和故障等......只是不超过 1 个用户或会话

我最初在 azure 中设置了一个应用程序洞察资源,并将其链接到我的 API 应用程序服务,但由于这没有显示更多用户,所以我随后在 VS 中将 azure 应用程序洞察配置到我的项目中,并将其与我使用的现有 Instrumentation Key 链接起来我正在使用

https://www.youtube.com/watch?v=yhGu3aIiMdo&t=109s

我发现了与计数用户类似的问题https://learn.microsoft.com/en-us/troubleshoot/azure/azure-monitor/app-insights/usage-troubleshoot#counting-users

关于

All telemetry events in Application Insights have an anonymous user ID and a session ID as two of their standard properties.  By default, all of the usage analytics tools count the users and sessions based on these IDs. If these properties aren't being populated with unique IDs for each user and session of your app, you'll see an incorrect count of users and sessions in the usage analytics tools.

所以我尝试在代码后面设置它,无论如何,会话ID我找不到要设置的匿名用户ID。

var telemetryClient = new TelemetryClient();
telemetryClient.InstrumentationKey = "123";

var ipAddress = HttpContext.Connection.RemoteIpAddress.ToString();
if (ipAddress == "::1")
{
   ipAddress = Dns.GetHostEntry(Dns.GetHostName()).AddressList[1].ToString();
}

telemetryClient.Context.User.Id = ipAddress;
telemetryClient.Context.User.AuthenticatedUserId = ipAddress;
telemetryClient.Context.Session.Id = ipAddress;


logger.LogInformation("User.Id -> " + ipAddress);
logger.LogInformation("User.AuthenticatedUserId -> " + ipAddress);
logger.LogInformation("Session.Id -> Ip -> " + ipAddress);

然后在我的 Kudu 日志中我可以看到:

Session.Id -> Ip -> //ip address of first user
...
Session.Id -> Ip -> //ip address of second user

但应用程序洞察的 UI 仪表板中仍然只有一个会话和一个用户

请问有什么帮助吗?

更新 只是为了澄清一下,我在一个应用程序服务上运行 React Node.js,在另一个应用程序服务上运行 C# API

c# azure-web-app-service azure-application-insights azure-log-analytics azure-monitoring
1个回答
0
投票

您正在使用 logger.LogInformation() 记录 IP 地址,但这不会使用您设置的会话 ID 将遥测数据发送到 Application Insights。会话 ID 将仅附加到根据您给定的代码使用 telemetryClient 实例发送的遥测数据。

Application Insights 中会话的概念主要是为 Web 应用程序设计的。虽然您可以将它与 API 一起使用,但它可能并不总是完美适合。

在 Web API 中,当遥测数据发送到 Application Insights 时,它可以包含 Session.Id 属性。如果您的遥测未提供会话 ID,Application Insights 不会自动为您创建会话 ID。这与 Web 应用程序 (UI) 不同,在 Web 应用程序中,Application Insights JavaScript SDK 可以根据用户活动自动生成和管理会话 ID。

如果您想通过 API 使用会话功能,则需要生成会话 ID。一种常见的方法是为连接到 API 的每个客户端生成一个新的会话 ID,并将该 ID 包含在该客户端“会话”期间发送的所有遥测数据中。这可以帮助您跟踪特定客户在特定时期内采取的所有操作。但由于您只是向 Web API 添加应用程序洞察,因此如果您想要自定义或覆盖会话 ID,可以通过配置遥测初始值设定项来实现。

以下是更新 ASP.NET Core 中遥测收集行为所使用的会话 ID 的方法:

  1. 创建自定义遥测初始化程序

    • 遥测初始化程序允许您在将遥测数据发送到 Application Insights 之前对其进行自定义。
    • 创建自定义遥测初始值设定项以设置会话 ID。
    using Microsoft.ApplicationInsights.Channel;
    using Microsoft.ApplicationInsights.Extensibility;
    
    public class CustomSessionTelemetryInitializer : ITelemetryInitializer
    {
        public void Initialize(ITelemetry telemetry)
        {
            // Generate or retrieve your custom session ID here
            string customSessionId = "YourCustomSessionId"; // Replace with your logic
    
            telemetry.Context.Session.Id = customSessionId;
        }
    }
    
  2. 注册自定义遥测初始化程序

    • 创建自定义遥测初始值设定项后,需要将其注册到
      Startup.cs
      文件中的 Application Insights 服务。
    using Microsoft.ApplicationInsights.Extensibility;
    
    public void ConfigureServices(IServiceCollection services)
    {
        // ... other services
    
        services.AddSingleton<ITelemetryInitializer, CustomSessionTelemetryInitializer>();
        services.AddApplicationInsightsTelemetry();
    }
    
© www.soinside.com 2019 - 2024. All rights reserved.