结合使用最新版本的Application Insight和.net核心API [英] Use latest version of Application Insight with .net core API
问题描述
我在.net core 3.0中有一个现有的API项目,并且还使用异常中间件记录了异常.现在,我们需要在API中实现应用程序见解.我经历了许多链接,但是对于某些新版本(例如app.UseApplicationInsightsExceptionTelemetry() and UseApplicationInsightsRequestTelemetry
),已经有些方法已经过时了.甚至我尝试在中间件的catch块中使用TelemetryClient
,但它已经过时了.因此,能否请您提供足够的信息以使我可以记录该异常.下面是一些代码片段,我需要在其中将错误记录到应用程序洞察力中.
在启动时,在方法public void ConfigureServices(IServiceCollection services)
内添加应用程序见解,如下所示:
services.AddApplicationInsightsTelemetry();
现在您可以像这样使用TelemetryClient
:
public class CustomMiddleware
{
private readonly RequestDelegate _next;
public CustomMiddleware(RequestDelegate next)
{
_next = next;
}
public async Task InvokeAsync(HttpContext context, TelemetryClient telemetryClient)
{
telemetryClient.TrackTrace($"Middleware {nameof(CustomMiddleware)} invoked");
await _next(context);
}
}
有关完整解决方案的信息,请参见此存储库 >
I have an existing API project in .net core 3.0 and also using exception middleware to log the exception. Now there is an requirement that we need to implement application insight in our API. I go through many links, but getting that some methods are already obsolete for newer version like app.UseApplicationInsightsExceptionTelemetry() and UseApplicationInsightsRequestTelemetry
. Even I tried to use TelemetryClient
inside the catch block of middleware, but again it is obsolete already. So could you please provide me the enough info that how can I log the exception. Below is the some code snippets where I need to log the error into application insight.
In your startup, inside the method public void ConfigureServices(IServiceCollection services)
add application insights like this:
services.AddApplicationInsightsTelemetry();
Now you can use the TelemetryClient
like this:
public class CustomMiddleware
{
private readonly RequestDelegate _next;
public CustomMiddleware(RequestDelegate next)
{
_next = next;
}
public async Task InvokeAsync(HttpContext context, TelemetryClient telemetryClient)
{
telemetryClient.TrackTrace($"Middleware {nameof(CustomMiddleware)} invoked");
await _next(context);
}
}
For a full solution see this repo
这篇关于结合使用最新版本的Application Insight和.net核心API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!