从 ASP.NET CORE WEB API 添加对应用程序洞察的请求

时间:2021-01-07 12:19:14

标签: azure-web-app-service azure-application-insights azure-webapps

我有一个 API 项目,我在 Azure Web 应用程序中运行了一段时间。但是,我在应用程序见解的请求表中没有看到任何请求。我错过了什么吗?

Startup.cs:

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Authorization;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using VendorFormAPI.Authentication;
using VendorFormAPI.Extensions;
using VendorFormData.Repositories;


namespace VendorFormAPI
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }


        public void ConfigureServices(IServiceCollection services)
        {
            services.AddApplicationInsightsTelemetry();            
            var dbConnStr = Configuration["Values:DBConnection"];

            services.AddTransient<VendorFormRepo, VendorFormRepo>((ctx) =>
            {

                var vfRepo = new VendorFormRepo(dbConnStr);
                return vfRepo;
            });

            services.AddTransient<FilesRepo, FilesRepo>((ctx) =>
            {
                var strgConnStr = Configuration["Values:StorageConnection"];
                var vfFilesRepo = new FilesRepo(strgConnStr, dbConnStr);
                return vfFilesRepo;
            });

            services.AddCors(options =>
            {
                options.AddDefaultPolicy(
                    builder =>
                    {
                        builder.WithOrigins("http://localhost:3000").AllowAnyHeader().AllowAnyMethod();
                    });
            });

            services.AddControllers();
            services.AddAuthentication(o => o.AddScheme("ApiKey", a => a.HandlerType = typeof(ApiKeyAuthenticationHandler)));         
        }

        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.ConfigureExceptionHandler();

            app.UseHttpsRedirection();

            app.UseRouting();
            app.UseCors(); // second

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }
    }
}

appsettings.cs:

{
  "Logging": {
    "ApplicationInsights": {
      "LogLevel": {
        "Default": "Trace",
        "System": "None",
        "Microsoft": "None"
      }
    }
  },
  "ApplicationInsights": {
    "Instrumentationkey": "xx-xx-4687-b1fc-xx"
  },
  "AllowedHosts": "*",
  "Values": {      
        }
}

Azure 中的配置: 已设置:APPINSIGHTS_INSTRUMENTATIONKEY=xxx

1 个答案:

答案 0 :(得分:0)

如果没有任何内容发送到 Application Insights 实例,原因有很多:

1.请检查 INSTRUMENTATIONKEY 是否正确。

2.有时,人们拥有不止 1 个 Application Insights。他们可能会使用 INSTRUMENTATIONKEY 为 Application_Insights_1 发送数据,但请检查 Application_Insights_2 中的结果。

3.如果没有特殊要求,将Application Insights的包更新到最新的。

4.检查你的代码,看看是否有一些过滤逻辑用于过滤日志。

5.日志级别设置不正确,比如设置为None

6.检查是否在 azure 门户或您的代码中启用了采样。

7.检查您的应用洞察Daily cap settings。如果达到限制,数据可能会被放弃。

这里有一些建议:

1.你最好在本地运行项目。例如,您可以在 Visual Studio 中运行它,并在 Visual Studio 输出窗口中检查输出。如果数据已发送,您可以在输出窗口中看到它们并带有正确的 INSTRUMENTATIONKEY

2.如果部署到azure,可以立即看到Live metrics中的数据。

相关问题