asp.net-web-api – 将Application Insight与ASP API Core结合使用
|
社区 我在将Application Insights连接到ASP WEB API Core时遇到了麻烦. 一旦我无法使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 – 当你不能使用ViewState时你会怎么做?
- asp.net – 在gridview中显示2位小数位数
- asp.net-mvc – 为什么MVC4捆绑捆绑Knockout.js?
- asp.net核心 – 如何在asp.net core mvc项目中解决“查看未
- 使用ASP.NET成员资格提供程序的现成安全令牌服务(STS)?
- asp.net-mvc – 如何使更多MapHttpRoutes为MVC 4 Api
- 在ASP.NET Web App中查找内存泄漏
- asp.net – ReportViewer阻止其他功能,直到报告查看器的加载
- ASP.NET和C#重定向
- asp.net – %#Eval(“State”)%或%#DataBinder.Eval(Con
