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 – 在SAAS体系结构中,如何处理多租户的数据库模式和
- ASP.NET Web Service中XML文档中的非法字符
- asp.net-mvc-4 – 调用客户端时应该发出服务器端方法异步?
- 使用Asp.Net Identity 2在AspNetUserClaims中存储用户信息有
- asp.net – 如何在MVC4的部分视图中添加脚本?
- iis-7 – ASP.NET应用程序吃内存. Application / Session对
- 为asp.net电子商务项目选择云
- asp.net-mvc-5 – aspnet身份避免同时登录同一帐户
- 如何在Asp.Net MVC 2中通过JQuery调用重载的C#函数
- asp.net-mvc-3 – MvcContrib网格和复选框
- asp.net – Dropdownlist在Page_ClientValidate(
- asp.net – 如何从System.Web.HttpPostedFileBas
- asp.net-mvc-3 – 是否可能获得一个ASP.NET MVC
- asp.net-mvc – 在ASP.NET MVC中的默认成员资格提
- asp.net-mvc-3 – Foreach ViewBag数据给出’对象
- asp.net-mvc – 如何最好地使用ASP.NET MVC中的E
- asp.net-mvc-3 – MVC 3.0 RC创建新项目空引用错
- asp.net – 在DataTable.Select中使用BETWEEN
- asp.net – 增加IIS7 / Win7 Pro上的最大上传文件
- asp.net-mvc – 如何让MVC POST返回上一页?