从.NET Core向Application Insights跟踪添加自定义维度

如何从.NET Core向Application Insights跟踪添加自定义维度? 欢迎任何指针。

always1988 回答:从.NET Core向Application Insights跟踪添加自定义维度

如果它是.net核心Web项目,则可以使用ITelemetryInitializer添加自定义维度。

首先,将一个名为MyTelemetryInitializer的新类添加到项目中:

public class MyTelemetryInitializer: ITelemetryInitializer
{
    public MyTelemetryInitializer()
    {
    }

    public void Initialize(ITelemetry telemetry)
    {

        if (telemetry is TraceTelemetry traceTelemetry)
        {

            if (!traceTelemetry.Properties.ContainsKey("my_custom_1"))
            {
                //add the custom dimension here
                traceTelemetry.Properties["my_custom_1"] = "test 12346"; 
            }

        }

    }
 }

然后在Startup.cs-> ConfigureServices方法中,添加以下代码行:

services.AddApplicationInsightsTelemetry();
services.AddSingleton<ITelemetryInitializer,MyTelemetryInitializer>();

出于测试目的,在HomeController中,我具有以下Index方法来发送跟踪消息:

        public IActionResult Index()
        {
            TelemetryClient client = new TelemetryClient();
            client.TrackTrace("it is a trace message from index page");

            return View();
        }

最后,运行项目。然后导航至Azure门户->应用程序见解,您可以看到已添加自定义维度。

enter image description here

,

更好地转换为ISupportProperties

如果(遥测为 ISupportProperties traceTelemetry)

本文链接:https://www.f2er.com/3148089.html

大家都在问