【Azure 應用程式見解】 Application Insights 對App Service的支援問題

路邊兩盞燈發表於2021-03-25

【Azure 應用程式見解】 Application Insights 對App Service的支援問題

問題描述

Web App 釋出後, Application Insights 收集不到資料了

 

問題分析

在應用服務(App Service)中收集應用的監控資料(如Request,Exception,Trace等)時通過Agent(Application Insight Site Extension)來實現的。因更新迭代的關係,有些版本不再,或還沒有被支援。如Agent(Application Insight Site Extension 2.8.38)支援.NET Core 2.1 and 3.1, 而 .Net Core 2.2不再被繼續支援,而.Net 5卻還沒有被支援。

 

解決辦法

一:使用基於代理(agent-based)模式修改.Net Core的版本和Application Agent版本。如:

  •  ApplicationInsightsAgent_EXTENSIONVERSION=~2 同時 升級至.NET Core 3.1
  •  ApplicationInsightsAgent_EXTENSIONVERSION=2.8.37 同時 繼續使用.NET Core 2.2

 

二:使用基於程式碼模式的Application Insighs:

2.1 安裝Application Insights SDK,建議一直使用最新的穩定版 Application Insights SDK NuGet package for ASP.NET Core。開啟專案中的.csproj檔案,以如下示例為參考新增Package引用。

   <ItemGroup>
      <PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.16.0" />
    </ItemGroup>

 

2.2 在專案的Startup類中的 ConfigureServices()方法中,新增 services.AddApplicationInsightsTelemetry()。啟動Application Insights

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        // The following line enables Application Insights telemetry collection.
        services.AddApplicationInsightsTelemetry();

        // This code adds other services for your application.
        services.AddMvc();
    }

 

2.3 設定Application Insights的 instrumentation Connection String。 這裡有多種方式來設定Connection String, Connection String的值由Azure Application門戶提供

【Azure 應用程式見解】 Application Insights 對App Service的支援問題

a)  使用系統級環境變數

在系統中新增環境變數引數:APPLICATIONINSIGHTS_CONNECTION_STRING。

 

b)  在程式碼設定,使用TelemetryConfiguration物件或ApplicationInsightsServiceOptions

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        var configuration = new TelemetryConfiguration
        {
            ConnectionString = "InstrumentationKey=00000000-0000-0000-0000-000000000000;"
        };
        
        // The following line enables Application Insights telemetry collection.
        services.AddApplicationInsightsTelemetry(configuration);

        // This code adds other services for your application.
        services.AddMvc();
    }

或者

    public void ConfigureServices(IServiceCollection services)
    {
        var options = new ApplicationInsightsServiceOptions { ConnectionString = "InstrumentationKey=00000000-0000-0000-0000-000000000000;" };
        services.AddApplicationInsightsTelemetry(options: options);
    }

 

c)  使用配置檔案(官方推薦方式)

XML

<?xml version="1.0" encoding="utf-8"?>
<ApplicationInsights xmlns="http://schemas.microsoft.com/ApplicationInsights/2013/Settings">
    <ConnectionString>InstrumentationKey=00000000-0000-0000-0000-000000000000</ConnectionString>
</ApplicationInsights>

JSON(config.json)

{
  "ApplicationInsights": {
    "ConnectionString" : "InstrumentationKey=00000000-0000-0000-0000-000000000000;"
    }
  }

 

 

參考資料

How to set a connection stringhttps://docs.microsoft.com/en-us/azure/azure-monitor/app/sdk-connection-string?tabs=net#how-to-set-a-connection-string

Enable Application Insights server-side telemetry (no Visual Studio)https://docs.microsoft.com/en-us/azure/azure-monitor/app/asp-net-core#enable-application-insights-server-side-telemetry-no-visual-studio

Enable agent-based monitoring:https://docs.microsoft.com/en-us/azure/azure-monitor/app/azure-web-apps?tabs=netcore#enable-agent-based-monitoring

Microsoft.ApplicationInsights.AspNetCore package: https://www.nuget.org/packages/Microsoft.ApplicationInsights.AspNetCore

 

 

相關文章