Use latest version of Application Insight with .net core API

此生再无相见时 提交于 2020-06-17 14:42:30

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!