asp.net-web-api – 将Application Insight与ASP API Core结合使用

前端之家收集整理的这篇文章主要介绍了asp.net-web-api – 将Application Insight与ASP API Core结合使用前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
社区

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

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

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

UPD

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();
        }

解决方法

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

如果您在VS2017中创建一个新的asp.net核心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)

在< head>的底部标签.

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

@Html.ApplicationInsightsJavaScript(TelemetryConfiguration)

从_Layout.cshtml中用上面的行替换它们,你可以删除所有的行,如:

app.UseApplicationInsightsExceptionTelemetry();

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

VS2017的官方发行说明将此信息作为“known issues” for application insights中的一部分

猜你在找的asp.Net相关文章