You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Azure Web App内存排查:添加指定性能计数器遇AI性能提示

Fixing Performance Counter Configuration Issues in Azure Web Apps with Application Insights

Hey there, let's tackle this performance counter issue you're hitting with Azure Web Apps and Application Insights. I've dealt with similar scenarios before, so here's a breakdown of what's going on and how to fix it:

First, Understand Azure Web App Sandbox Limitations

Azure Web Apps run in a restricted sandbox environment—you don't have full access to system-level resources like you would on a dedicated VM. That's why some of your counter configs are throwing errors. Let's go through each counter you listed and address them one by one:

1. System-Level Memory Counters

Counters like \Memory\Page Faults /sec, \Memory\Page Reads /sec, \Memory\Cache Faults /sec, and \Memory\Available Bytes are generally supported, but your config syntax might be tripping things up. Try removing the leading backslash and adjusting spacing to match the exact counter name the system recognizes. For example, use Memory\Page Faults/sec instead of \Memory\Page Faults /sec (no space after the slash).

2. Paging File Counter

\Paging File(*)\% Usage Peak is a no-go in the Web App sandbox. The sandbox restricts access to system paging file details, so you won't be able to collect this. Instead, use alternative metrics like \Memory\Committed Bytes or the built-in Process\Private Bytes counter that Application Insights often collects by default to gauge memory pressure.

3. Process-Level Counters (Fix the Placeholder!)

Your \Process(??APP_W3SVC_PROC??)\Working Set Peak and \Process(??APP_W3SVC_PROC??)\Working Set - Private entries use a placeholder that doesn't work in the Web App environment. Replace ??APP_W3SVC_PROC?? with the actual process name of your web app—this is almost always w3wp.exe. So your counters should look like:

  • \Process(w3wp)\Working Set Peak
  • \Process(w3wp)\Working Set - Private

If you're unsure, you can use \Process(*)\Working Set Peak to collect data for all processes, but that might add unnecessary noise. Also, check if Application Insights is already collecting private working set data by default—you might not need to add it manually.

Correctly Configure ApplicationInsights.config

If modifying the config file is throwing errors, follow these steps to get it right:

  1. Update Your SDK First: Make sure you're using the latest version of the Application Insights Web SDK (v2.8 or newer). Older versions have limited support for Web App counters. Update via NuGet: Microsoft.ApplicationInsights.Web.

  2. Adjust the Counter Configuration: In your ApplicationInsights.config, find the TelemetryModules section and add (or update) the PerformanceCollectorModule with the corrected counter syntax:

<Add Type="Microsoft.ApplicationInsights.Extensibility.PerfCounterCollector.PerformanceCollectorModule, Microsoft.AI.PerfCounterCollector">
  <Counters>
    <Add PerformanceCounter="Memory\Page Faults/sec" ReportAs="Memory Page Faults per Second" />
    <Add PerformanceCounter="Memory\Page Reads/sec" ReportAs="Memory Page Reads per Second" />
    <Add PerformanceCounter="Memory\Cache Faults/sec" ReportAs="Memory Cache Faults per Second" />
    <Add PerformanceCounter="Memory\Available Bytes" ReportAs="Memory Available Bytes" />
    <Add PerformanceCounter="Process(w3wp)\Working Set Peak" ReportAs="Process Working Set Peak" />
    <Add PerformanceCounter="Process(w3wp)\Working Set - Private" ReportAs="Process Private Working Set" />
  </Counters>
</Add>

Double-check that each counter name matches exactly what the system uses—small typos (like extra spaces) will cause errors.

  1. Handle Sandbox Restrictions: If a counter still throws an error, it's likely blocked by the Web App sandbox. In that case, switch to alternative metrics:
    • Use Application Insights' built-in request, dependency, or exception telemetry to spot memory leaks indirectly.
    • Check the Azure Portal's Web App Metrics tab for pre-built memory metrics like "Memory Working Set" or "Private Bytes".

Alternative: Configure Counters via Code

If config file changes aren't working, try registering counters in code instead. This gives you more control and lets you catch errors at startup:

using Microsoft.ApplicationInsights.Extensibility.PerfCounterCollector;

public void ConfigureServices(IServiceCollection services)
{
    var perfCounterModule = new PerformanceCollectorModule();
    
    // Add each counter with corrected names
    perfCounterModule.Counters.Add(new PerformanceCounterCollectionRequest(
        "Memory\\Page Faults/sec", "Memory Page Faults per Second"));
    perfCounterModule.Counters.Add(new PerformanceCounterCollectionRequest(
        "Process(w3wp)\\Working Set Peak", "Process Working Set Peak"));
    // Add other counters here...

    // Register the module with Application Insights
    services.AddApplicationInsightsTelemetry();
    services.AddSingleton<ITelemetryModule>(perfCounterModule);
}

This way, if a counter isn't supported, you'll see an exception in your app logs that tells you exactly which one is causing the problem.

Verify Your Setup

After deploying changes:

  1. Check the Application Insights Metrics explorer to see if your new counters are showing up.
  2. Look at the Web App's Log Stream in the Azure Portal for any errors like "Performance counter not found"—this will help you troubleshoot unsupported counters.
  3. Ensure your Web App is on a pricing tier that supports performance counters (Free tier has limited support; Basic or higher is recommended).

内容的提问来源于stack exchange,提问作者zebra

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 08:04:39