有没有办法在 Application Insights 自定义事件中设置 appName?

Is there a way to set appName in Application Insights custom events?

我注意到在分析工具(见下文)中查询时,App Insights 有一个名为 appName(和 appId)的字段,但是我没有在客户端库中看到设置它的方法。这个可以设置吗?

我正在尝试将相关项目记录到应用洞察,但日志源可能不同。使用该字段似乎是处理该情况的好方法,尽管我对不同的情况持开放态度。


这些值在后端填充并指定 Application Insights 资源详细信息,因此无法更改。

您要找的是custom dimensions

例如,发送遥测数据:

EventTelemetry telemetry = new EventTelemetry("my custom event");
telemetry.Properties.Add("MyApp", "HelloWorld");
telemetryClient.TrackEvent(telemetry);

要查询那些自定义维度:

customEvents
| where customDimensions['MyApp'] == "HelloWorld"

有一个预览,您可以在其中设置应用程序名称。这是通过以下方式完成的:

  • 启用预览应用地图
  • 设置ITelemetry.Context.Cloud.RoleName(这是应用程序名称)

启用预览:转到门户 -> 应用程序洞察 -> 预览 -> 启用多角色应用程序地图

设置应用名称:

这是通过添加一个拦截器来完成的:

public class ServiceNameInitializer : ITelemetryInitializer
{
    public void Initialize(ITelemetry telemetry)
    {
        telemetry.Context.Cloud.RoleName = "MyProcessName";
        // RoleInstance property modifies the app name in the telmetry dashboard
        telemetry.Context.Cloud.RoleInstance = "MyAppInstanceName";
    }
}

将拦截器添加到 startup.cs 中的 application insights 配置:

private void ConfigureAppInsights(IServiceCollection services)
{
    services.AddApplicationInsightsTelemetry(Configuration);
    TelemetryConfiguration.Active.TelemetryInitializers
       .Add(new ServiceNameInitializer());
}

阅读更多信息:cross-process-application-insights-with-multi-role-application-map