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

在ASP.NET Core 2.0 Razor Pages中配置Application Insights追踪遇阻

解决ASP.NET Core 2.0 Razor Pages中Application Insights捕获Trace日志的问题

嘿,我来帮你搞定这个Application Insights追踪的问题!针对你在Visual Studio 2017里的ASP.NET Core 2.0 Razor Pages场景,咱们一步步排查和配置:

1. 确保安装了正确的Application Insights包

首先得确认你的项目里装了适配ASP.NET Core 2.0的Application Insights SDK:

  • 打开NuGet包管理器,搜索安装Microsoft.ApplicationInsights.AspNetCore,版本选2.14.0左右(这个版本完美适配.NET Core 2.0)
  • 或者在Package Manager Console里执行命令:
    Install-Package Microsoft.ApplicationInsights.AspNetCore -Version 2.14.0
    

2. 配置Startup.cs启用Application Insights

在Startup.cs里的ConfigureServices方法中,添加Application Insights服务注册:

public void ConfigureServices(IServiceCollection services)
{
    // 其他服务注册...
    services.AddApplicationInsightsTelemetry(Configuration);
}

这一步是让ASP.NET Core把Application Insights集成到整个应用的生命周期里。

3. 配置日志过滤,让Application Insights接收Trace类的输出

默认情况下,Application Insights不会自动捕获System.Diagnostics.Trace类的日志,需要在appsettings.json里添加日志级别配置:

{
  "ApplicationInsights": {
    "InstrumentationKey": "你的Azure Application Insights仪表密钥"
  },
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning"
    },
    "ApplicationInsights": {
      "LogLevel": {
        "System.Diagnostics.TraceSource": "Trace"
      }
    }
  }
}

这里把System.Diagnostics.TraceSource的日志级别设为Trace,确保所有Trace类输出的日志(包括你写的TraceError、TraceWarning等)都会被Application Insights捕获。

4. 优化日志写入方式(可选但推荐)

如果你想更直接地控制Application Insights的日志,建议直接注入TelemetryClient而不是用Trace类,这样能更精准地控制日志的级别和内容:
在你的Index.cshtml.cs里修改代码:

using Microsoft.ApplicationInsights;
using Microsoft.AspNetCore.Mvc.RazorPages;

namespace YourProjectNamespace.Pages
{
    public class IndexModel : PageModel
    {
        private readonly TelemetryClient _telemetryClient;

        // 通过构造函数注入TelemetryClient
        public IndexModel(TelemetryClient telemetryClient)
        {
            _telemetryClient = telemetryClient;
        }

        public void OnGet()
        {
            _telemetryClient.TrackTrace("*************** INDEX ERROR ***************", Microsoft.ApplicationInsights.DataContracts.SeverityLevel.Error);
            _telemetryClient.TrackTrace("*************** INDEX WARNING ***************", Microsoft.ApplicationInsights.DataContracts.SeverityLevel.Warning);
            _telemetryClient.TrackTrace("*************** INDEX INFORMATION ***************", Microsoft.ApplicationInsights.DataContracts.SeverityLevel.Information);
            _telemetryClient.TrackTrace("*************** INDEX WRITELINE ***************");
        }
    }
}

这种方式不需要依赖Trace类的配置,日志会直接发送到Application Insights。

5. 验证配置是否生效

运行你的项目,访问Index页面触发OnGet方法,然后:

  1. 打开Azure Portal,进入你的Application Insights资源
  2. 点击左侧的“日志”选项,输入查询语句:
    traces | where message contains "INDEX"
    
  3. 等待几分钟(本地调试的日志可能有延迟),就能看到你输出的日志了。

常见排查点

  • 确认appsettings.json里的InstrumentationKey是否和Azure里的Application Insights资源一致
  • 检查Startup.cs里有没有漏加services.AddApplicationInsightsTelemetry(Configuration)
  • 如果是本地调试,确保没有代理或防火墙阻止日志发送到Azure

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 08:14:38