gpt4 book ai didi

asp.net-core - 为自定义事件和指标注入(inject) App Insights

转载 作者:行者123 更新时间:2023-12-04 22:55:10 26 4
gpt4 key购买 nike

什么是“正确”的使用方式custom events and metrics for app insights在带有依赖注入(inject)的 ASP.NET Core 中?有没有办法注入(inject)TelemetryClient ?

我能找到的所有东西都实例化了 TelemetryClient直接和 TelemetryClient没有实现接口(interface)。

最佳答案

当您使用 .UseApplicationInsights() 配置 Application Insights 时,TelemetryClient 会自动注入(inject) DI或 AddApplicationInsights()方法。您可以使用构造函数注入(inject)来获取 TelemetryClient实例如下图。

public class HomeController : Controller
{
private TelemetryClient telemetry;

public HomeController(TelemetryClient telemetry)
{
this.telemetry = telemetry;
}

public IActionResult Index()
{
this.telemetry.TrackEvent("HomePageRequested");
return View();
}
}

https://github.com/Microsoft/ApplicationInsights-aspnetcore/wiki/Custom-Configuration#track-custom-traceeventmetric

关于asp.net-core - 为自定义事件和指标注入(inject) App Insights,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50916081/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com