简体   繁体   English

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

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

I created app-insight application in azure to keep track of metrics.我在 azure 中创建了 app-insight 应用程序来跟踪指标。 Everything looks fine and easy to see Req/Res.I did below steps: adding appsettings.json:一切看起来都很好,很容易看到 Req/Res。我做了以下步骤:添加 appsettings.json:

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

And adding UseApplicationInsights in Program.cs并在 Program.cs 中添加 UseApplicationInsights

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

but I want to send something to appinsight and see as a column when I write kusto query.但是我想在编写 kusto 查询时向 appinsight 发送一些内容并作为一列查看。

is there any way like below stuff:有没有像下面这样的东西:

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

Can I do that or any way to send or modify existed value?我可以这样做或以任何方式发送或修改现有值吗? I Tried to find any article about it but I couldn't satisfied solution.我试图找到任何关于它的文章,但我无法满足解决方案。

You can add it as a custom property via ITelemetryInitializer .您可以通过ITelemetryInitializer将其添加为自定义属性。

In the sample code, you just need a few modification to add a property like below:在示例代码中,您只需进行一些修改即可添加如下属性:

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

 }

For how to register the ITelemetryInitializer to asp.net core project, please refer to this section " ASP.NET Core/ Worker Service apps: Load your initializer " in the above doc.关于如何将 ITelemetryInitializer 注册到 asp.net 核心项目,请参阅上述文档中的“ ASP.NET Core/Worker Service apps: Load your initializer ”一节。 A screenshot as below:截图如下:

在此处输入图像描述

After you add the above code, then every telemetry data will include the custom property.添加上述代码后,每个遥测数据都将包含自定义属性。 Then in your kusto query, you can take use of this property.然后在您的 kusto 查询中,您可以使用此属性。

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

相关问题 如何在 .NetCore 2.2 中从 StartUp 注入 2 EventHubClient - How to Inject 2 EventHubClient from StartUp in .NetCore 2.2 如何在 netcore 2.2 中使用 Basic Auth - How to use Basic Auth in netcore 2.2 如何在蛋糕中编译.netcore 2.2项目 - How to compile .netcore 2.2 project in cake 如何从Lambda向.NetCore中的SQS发送kinesisEvent故障 - How do I send a kinesisEvent failure from Lambda to SQS in .NetCore 如何在 exceptionFilter Asp.NetCore 中获取模型对象? - how can i get model object in exceptionFilter Asp.NetCore? 如何在 netcore Web 应用程序中创建任务包装器或中间件? - How can I create a Task wrapper or middleware in a netcore web application? Netcore 2.2本地化路由-如何始终重定向到默认路由 - Netcore 2.2 Localized Routing - How to always redirect to default route 如何使用 .netcore 中的 SecurityTokenDescriptor 将孩子添加到 jwt 标头 - How can I add kid to jwt header using SecurityTokenDescriptor in .netcore 我怎样才能看到团队中谁在使用TFS编写的代码最多,谁在编写的代码最少? - How can I see who in my team are writing the most code and who is writing the least using TFS? 通过代码,我怎样才能让硬盘驱动器进入睡眠状态 - By code, how can I send a hard disk drive to sleep
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM