asp.net - 无法将 ASP.NET Core Web 应用程序与 Application Insights 连接

标签 asp.net asp.net-mvc azure asp.net-core azure-application-insights

我在 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/

相关文章:

ASP.NET Web API : The requested resource does not support http method 'GET'

c# - 添加 ViewModel 类将代码优先模型添加到数据库

c# - 如何在blob存储中创建文件夹

c# - 为某些 Action MVC 添加相同的 AuthorizeAttribute 时如何覆盖全局 AuthorizeAttribute?

azure - Visual Studio Online 摩纳哥在哪里?

python - 为什么我使用 Windows 身份验证通过 AzureML dataprep 登录 MS SQL 失败?

c# - Codebehind 不创建按钮单击事件

c# - 如何通过 WCF 设置 SSL?

c# - 允许用户下载文件

asp.net-mvc - 我们什么时候应该实现自定义 MVC ActionFilter?