繁体   English   中英

如何通过在 .netcore 2.2 中编写代码将 CustomUserId 发送到 Appinsight?

[英]How can I send CustomUserId to Appinsight by writing code in .netcore 2.2?

我在 azure 中创建了 app-insight 应用程序来跟踪指标。 一切看起来都很好,很容易看到 Req/Res。我做了以下步骤:添加 appsettings.json:

"ApplicationInsights": {
"InstrumentationKey": "xxxx-7a49-4bc1-yyyy-kkkkk"
},

并在 Program.cs 中添加 UseApplicationInsights

  public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
        .UseApplicationInsights()
            .UseStartup<Startup>();

但是我想在编写 kusto 查询时向 appinsight 发送一些内容并作为一列查看。

有没有像下面这样的东西:

 public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
        .UseApplicationInsights()
        .ChangeValue("UserId",Httphelper.GetCurrentUserId)
        .AddColumn("MyCustomField", Httphelper.Something)
            .UseStartup<Startup>();

我可以这样做或以任何方式发送或修改现有值吗? 我试图找到任何关于它的文章,但我无法满足解决方案。

您可以通过ITelemetryInitializer将其添加为自定义属性。

在示例代码中,您只需进行一些修改即可添加如下属性:

  public class MyTelemetryInitializer : ITelemetryInitializer
  {
    public void Initialize(ITelemetry telemetry)
    {
      telemetry.Properties["customuserid"] = "your_id";
    }

 }

关于如何将 ITelemetryInitializer 注册到 asp.net 核心项目,请参阅上述文档中的“ ASP.NET Core/Worker Service apps: Load your initializer ”一节。 截图如下:

在此处输入图像描述

添加上述代码后,每个遥测数据都将包含自定义属性。 然后在您的 kusto 查询中,您可以使用此属性。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM