Events 只有当客户机在遥测配置的Using语句中实例化时,才能在ApplicationInsights中使用遥测客户机
我正在使用ApplicationInsights,定义并发送自己的自定义事件 我使用遥测客户端来实现这一点。 仅当我按照以下方式实例化并使用遥测客户端对象时,它才起作用:Events 只有当客户机在遥测配置的Using语句中实例化时,才能在ApplicationInsights中使用遥测客户机,events,sdk,azure-application-insights,telemetry,Events,Sdk,Azure Application Insights,Telemetry,我正在使用ApplicationInsights,定义并发送自己的自定义事件 我使用遥测客户端来实现这一点。 仅当我按照以下方式实例化并使用遥测客户端对象时,它才起作用: TelemetryClient telemetryClient; using (var telemetryConfiguration = new TelemetryConfiguration("instrumentationKey")) { telemet
TelemetryClient telemetryClient;
using (var telemetryConfiguration = new TelemetryConfiguration("instrumentationKey"))
{
telemetryClient = new TelemetryClient(telemetryConfiguration);
telemetryClient.TrackEvent("CustomEvent1");
telemetryClient.Flush();
Thread.Sleep(5000);
}
问题是,我想在不同的服务中注入TeletRyclient。但在同一位置调用此调用不会在门户中生成任何事件:
TelemetryClient telemetryClient;
using (var telemetryConfiguration = new TelemetryConfiguration("instrumentationKey"))
{
telemetryClient = new TelemetryClient(telemetryConfiguration);
}
telemetryClient.TrackEvent("CustomEvent1");
telemetryClient.Flush();
Thread.Sleep(5000);
这是使用TeletRyclient的错误方法吗?如果您正在编写.Net核心应用程序,则可以在Startup.cs的ConfigureServices方法中配置遥测客户端的依赖项注入。 有关完整示例,请参见
public void ConfigureServices(IServiceCollection services)
{
...
services.AddApplicationInsightsTelemetry();
...
}
然后,如果您正在编写一个Mvc应用程序,例如,您可以在控制器中注入遥测客户端,如下所示:
private readonly TelemetryClient tc;
public MyController(TelemetryClient _tc)
{
tc = _tc;
}
public HttpResponseMessage Get(int id)
{
tc.TrackEvent("CustomEvent1");
...
}
请确保正确配置appsettings.json:
"ApplicationInsights": {
"InstrumentationKey": "..." }
希望这有帮助,
安德烈亚斯