Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将遥测发送到ConfigureServices内部的ApplicationInsights_C#_Asp.net Core_.net Core_Azure Application Insights - Fatal编程技术网

C# 将遥测发送到ConfigureServices内部的ApplicationInsights

C# 将遥测发送到ConfigureServices内部的ApplicationInsights,c#,asp.net-core,.net-core,azure-application-insights,C#,Asp.net Core,.net Core,Azure Application Insights,我想从ConfigureServices内部发送一些遥测数据,例如,如果我缺少一个配置设置,我想向AI发送一个事件来跟踪它 public class Startup { public void ConfigureServices( IServiceCollection services) { services.AddApplicationInsightsTelemetry(); // Is there a way of getting the telemet

我想从ConfigureServices内部发送一些遥测数据,例如,如果我缺少一个配置设置,我想向AI发送一个事件来跟踪它

public class Startup
{
   public void ConfigureServices( IServiceCollection services)
   {
      services.AddApplicationInsightsTelemetry();

      // Is there a way of getting the telemetry client here to send some telemetry?
      // e.g. TelemetryClient.TrackEvent("MyEvent");
   }
}

是否有任何方法可以在ConfigureServices中获取由
AddApplicationInsightsTelemetry
创建的遥测客户端?上下文在该点是否有效?

否,在
ConfigureServices
方法完成之前,我们无法在
ConfigureServices
中获取由
AddApplicationInsightsTelemetry
创建的遥测客户端

您应该在
ConfigureServices
方法中手动创建遥测客户端,如下所示:

public void ConfigureServices(IServiceCollection services)
{
    services.AddControllersWithViews();
    var s1 = services.AddApplicationInsightsTelemetry(Configuration["APPINSIGHTS_INSTRUMENTATIONKEY"]);

    TelemetryConfiguration configuration = TelemetryConfiguration.CreateDefault();
    configuration.InstrumentationKey = Configuration["APPINSIGHTS_INSTRUMENTATIONKEY"];
    var telemetryClient = new TelemetryClient(configuration);

    telemetryClient.TrackEvent("xxx");
}