我在 Azure 应用服务托管的 Visual Studio Community 2015 Update 3 中有 ASP.NET Core MVC 项目(作为控制台应用程序启动的新奇事物之一)。我正在尝试设置 Application Insights,主要是因为我想使用 ApplicationInsightsTraceListener
捕获诊断跟踪,但其他功能也很有用。我还没有找到正确的配置来让任何东西显示在 Azure 门户中。
我使用 Visual Studio 扩展向我的项目添加了 Application Insights 支持。然而,这似乎是事情开始出错的地方,它没有创建一个ApplicationInsights.config
。它添加了一些 DLL,并将 InstrumentationKey
放入我的应用设置文件中,并且我通过 NuGet 手动添加了更多程序集,包括 Microsoft.ApplicationInsights.Web
和 Microsoft.ApplicationInsights.TraceListener
。此时发布会导致 Azure 门户中未启用任何指标,并且实时流告诉我不可用:您的应用程序已离线或使用较旧的 SDK。
我已经尝试过 2.1.0 和 2.2.0 beta。我尝试删除所有 Application Insights 内容,重新安装扩展并重新开始。我还尝试跳过使用扩展,手动添加 Microsoft.ApplicationInsights.Web
等,一些谷歌结果暗示也应该创建配置文件,但没有运气。我从网上下载了其他人的 ApplicationInsights.config
文件,并尝试使它们适合我的环境。门户的 Application Insights 部分从来没有任何工作。
除了创建配置文件之外,扩展程序还应该采取哪些额外步骤?我需要向 Startup.cs
添加代码吗?大多数文档似乎理所当然地认为初始设置有效。
最佳答案
考虑到您的 InstrumentationKey
位于 appsettings.json
上,您需要修改 Startup.cs
:
public void ConfigureServices(IServiceCollection services)
{
/*Simple Mvc web app*/
services.AddMvc();
services.AddApplicationInsightsTelemetry(Configuration);
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env, IAntiforgery antiforgery)
{
/*This should be before UseMvc*/
app.UseApplicationInsightsRequestTelemetry();
app.UseApplicationInsightsExceptionTelemetry();
app.UseMvc();
}
重要的是,app.UseApplicationInsightsRequestTelemetry()
位于 app.UseMvc()
之前。
关于asp.net - 无法将 ASP.NET Core Web 应用程序与 Application Insights 连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39274082/