使用最新版本的Application Insight与.net核心API。

问题描述 投票:0回答:1

我有一个现有的.net core 3.0的API项目,也使用异常中间件来记录异常。现在有一个需求,我们需要在我们的API中实现应用洞察力。我看了很多链接,但是有些方法在新版本中已经过时了,例如 app.UseApplicationInsightsExceptionTelemetry() and UseApplicationInsightsRequestTelemetry. 甚至我还想用 TelemetryClient 在中间件的捕获块内,但它又已经过时了。所以你能不能给我提供足够的信息,让我如何记录这个异常。下面是一些代码片段,我需要将错误记录到应用程序的洞察力。

enter image description here

visual-studio asp.net-web-api asp.net-core-webapi azure-application-insights asp.net-core-3.0
1个回答
3
投票

在你的启动过程中,在方法 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);
        }
    }

完整的解决方案见 这个回购

© www.soinside.com 2019 - 2024. All rights reserved.