问题
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.
回答1:
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
来源:https://stackoverflow.com/questions/61772015/use-latest-version-of-application-insight-with-net-core-api