spo*_*ahn 2 dependency-injection azure-application-insights asp.net-core
在具有依赖项注入的ASP.NET Core中,使用自定义事件和指标获取应用程序见解的“正确”方法是什么?有没有办法注入TelemetryClient?
我能找到的所有内容都TelemetryClient直接实例化了实例,并且TelemetryClient没有实现接口。
使用.UseApplicationInsights()或AddApplicationInsights()方法配置Application Insights时,TelemetryClient会自动注入到DI中。您可以使用构造函数注入来获取TelemetryClient实例,如下所示。
public class HomeController : Controller
{
private TelemetryClient telemetry;
public HomeController(TelemetryClient telemetry)
{
this.telemetry = telemetry;
}
public IActionResult Index()
{
this.telemetry.TrackEvent("HomePageRequested");
return View();
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2505 次 |
| 最近记录: |