【问题标题】:Use Application Insight with ASP API Core将 Application Insight 与 ASP API 核心一起使用
【发布时间】:2017-02-28 15:59:45
【问题描述】:

社区

我在将 Application Insights 连接到我的 ASP WEB API 核心时遇到问题。 按照标准手册,我仍然无法在我的 AppInsights 帐户中找到任何记录。 我使用了很多手册,但它们几乎完全相同,并描述了如何为 ASP Core(而不是 API Core)配置 App Insights。 所以我想知道是否需要一些特殊配置(或 nuget 包或其他)来使 AppInsights 跟踪对我的 API 服务的请求?

一旦我无法让 AppInsights 开箱即用,我仍然可以创建 TelemetryClient 的实例并手动发布数据,但这在我的情况下是不可取的。

重要提示:我使用的是 VS 2017 RC,Web APi Core 项目 (csproj)

更新

csproj 文件内容:

  <Project ToolsVersion="15.0" Sdk="Microsoft.NET.Sdk.Web">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp1.0</TargetFramework>
    <PreserveCompilationContext>true</PreserveCompilationContext>
  </PropertyGroup>
  <ItemGroup>
  <ItemGroup>
    <DotNetCliToolReference Include="Microsoft.DotNet.Watcher.Tools" Version="1.0.0-msbuild1-update1" />
    <DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="1.0.0-msbuild1-final" />
    <DotNetCliToolReference Include="Microsoft.Extensions.SecretManager.Tools" Version="1.0.0-msbuild2-update1" />
    <DotNetCliToolReference Include="Microsoft.DotNet.Watcher.Tools" Version="1.0.0-msbuild1-update1" />
    <DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="1.0.0-msbuild1-final" />
    <DotNetCliToolReference Include="Microsoft.Extensions.SecretManager.Tools" Version="1.0.0-msbuild2-update1" />
    <PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="1.0.1" />
    <PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="1.0.1" />
    <PackageReference Include="Microsoft.ApplicationInsights" Version="2.2.0" />
    <PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="1.0.1" />
    <PackageReference Include="Microsoft.AspNetCore.Cors" Version="1.0.1" />
    <PackageReference Include="Microsoft.AspNetCore.ResponseCompression" Version="1.0.0" />
    <PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="1.0.1" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="1.0.0-msbuild1-final" />
    <PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning" Version="1.0.3" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="1.0.1" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="1.0.0" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer.Design" Version="1.0.0" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="1.0.0-msbuild2-final" />
    <PackageReference Include="Microsoft.NETCore.App" Version="1.0.1" />
    <PackageReference Include="Microsoft.AspNetCore.Diagnostics" Version="1.0.1" />
    <PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.0.1" />
    <PackageReference Include="Microsoft.AspNetCore.Routing" Version="1.0.1" />
    <PackageReference Include="Microsoft.AspNetCore.Server.IISIntegration" Version="1.0.0" />
    <PackageReference Include="Microsoft.AspNetCore.Server.Kestrel" Version="1.0.1" />
    <PackageReference Include="Microsoft.AspNetCore.Server.Kestrel.Https" Version="1.0.1" />
    <PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="1.0.1" />
    <PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="1.0.1" />
    <PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="1.0.1" />
    <PackageReference Include="Microsoft.Extensions.Logging" Version="1.0.0" />
    <PackageReference Include="Microsoft.Extensions.Logging.Console" Version="1.0.0" />
    <PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="1.0.0" />
    <PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="1.0.1" />
    <PackageReference Include="Newtonsoft.Json" Version="9.0.1" />
    <PackageReference Include="Swashbuckle.SwaggerGen" Version="6.0.0-beta901" />
    <PackageReference Include="Swashbuckle.SwaggerUi" Version="6.0.0-beta901" />
  </ItemGroup>
</Project>

Startup.cs 中的配置:

    public class Startup
    {
        public Startup(IHostingEnvironment env)
        {
            var builder = new ConfigurationBuilder()
                .SetBasePath(env.ContentRootPath)
                .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true);

            if (env.IsDevelopment())
            {
                // This will push telemetry data through Application Insights pipeline faster, allowing you to view results immediately.
                builder.AddApplicationInsightsSettings(true);
            }

            builder.AddEnvironmentVariables();

            Configuration = builder.Build();
        }

        public IConfigurationRoot Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc();

            services.AddApplicationInsightsTelemetry(Configuration);
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            loggerFactory.AddConsole(Configuration.GetSection("Logging"));
            loggerFactory.AddDebug(LogLevel.Trace);
            loggerFactory.AddConsole(LogLevel.Error);

            app.UseApplicationInsightsExceptionTelemetry();

            app.UseMvc();
        }

【问题讨论】:

  • 仅供参考:没有 ASP.NET Web API Core 这样的东西。 MVC 和 Web API 已合并到 ASP.NET Core MVC 中。您能否向我们展示一些包含日志记录和您的 csproj 文件的代码?

标签: asp.net-web-api asp.net-core azure-application-insights


【解决方案1】:

如果你在 VS2017 中使用“新”的 asp.net 核心,那么旧指令是错误的,因为它们适用于以前基于 xproj 的 asp.net 核心实现。

如果您在 VS2017 中创建 new asp.net core web 项目,ApplicationInsights 将从一开始就已经安装,并且应该是版本:

<PackageReference Include="Microsoft.ApplicationInsights" Version="2.2.0" />
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.0.0" />

(或更新,如果 asp.net 核心团队已经更新了它们)

这些项目也已经连接了 Application Insights,而不是在 Startup.cs(这是旧方式)中,而是在 Program.cs 中:

new WebHostBuilder()
   ...
   .UseApplicationInsights() // this starts up appinsights in asp.net core now
   ...
   .UseOtherThings();

可能在网页模板中,例如:

 @inject Microsoft.ApplicationInsights.AspNetCore.JavaScriptSnippet JavaScriptSnippet

在顶部,并且

 @Html.Raw(JavaScriptSnippet.FullScript)

&lt;head&gt; 标签的底部。

如果您要从以前版本的 asp.net 核心和应用洞察力迁移,您还必须删除以下内容:

@Html.ApplicationInsightsJavaScript(TelemetryConfiguration) 

来自 _Layout.cshtml 并将它们替换为上面的行,您可以删除所有行,例如:

app.UseApplicationInsightsExceptionTelemetry();

在 Startup.cs 中(如果您使用的是 2.x 版本的软件包,我相信这些项目也会显示弃用警告,因为它们不再需要)

VS2017 的官方发行说明将此信息作为一个部分包含在 "known issues" for application insights

【讨论】:

  • 对于一个MVC web api(VS2017 Web Api project /MVC core 1.1),是否只是在appsettings.json中添加InstrumentationKey?
  • 如果你是在 VS2017 中创建的,那么是的,AI 应该已经包含在项目中,你只需要设置 ikey,或者通过在 appsettings.json 中手动设置,或者通过右侧设置点击项目,选择configure application insights,然后有 UI 可以设置它。
  • 经过数小时的搜索后,我终于可以让 App Insights 在 Azure 中为 Asp.Net Core Web API 应用程序工作 - 非常感谢。
【解决方案2】:

感谢article,问题解决了

我在配置中遗漏了 2 点:
1.在csproj中应该引用包“Microsoft.ApplicationInsights.AspNetCore”Version="2.0.0-beta1"
2.在Startup类中,方法Configure我错过了添加app.UseApplicationInsightsRequestTelemetry();

当我更改上述内容时,AppInsights 已开始跟踪所有请求

【讨论】:

  • 我在某处读到我们应该使用Program.cs 方法而不是Configure 方法来添加它。我在两者中都有它们并且得到了双倍的遥测。
猜你喜欢
  • 2020-08-29
  • 1970-01-01
  • 1970-01-01
  • 2022-01-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-26
相关资源
最近更新 更多