【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门户提供

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

 

 

posted @   路边两盏灯  阅读(179)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示