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

Azure Application Insights无法显示ASP.NET Core 7.0的C# ILogger日志

ASP.NET Core 7.0中Application Insights自定义ILogger日志不显示在Traces区域的问题

我们正尝试为ASP.NET Core 7.0 C#应用启用Azure Application Insights,已参考包括微软官方文档在内的多个资料。目前Azure Application Insights模块配置看似正常,例如可正确显示应用请求,但应用中的自定义ILogger日志并未出现在Azure App Insights的traces区域。我们参考多种示例完成以下配置,不确定问题出在哪,怀疑是代码配置问题。

C# Program代码

public static void Main(string[] args)
{
    var builder = WebApplication.CreateBuilder(args);

    ...
    
    builder.Services.AddApplicationInsightsTelemetry();

    builder.Logging.AddApplicationInsights(
        configureTelemetryConfiguration: config =>
             config.ConnectionString = configuration["APPLICATIONINSIGHTS_CONNECTION_STRING"],
        configureApplicationInsightsLoggerOptions: options => { }
    );
    
    ... 

    var app = builder.Build();
}        

appsettings.json配置

"Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning",
      "ApplicationInsights": "Debug"
    }
  }

csproj文件配置

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

Azure Web应用设置配置

已在Azure Web应用中配置APPLICATIONINSIGHTS_CONNECTION_STRING


可能的排查方向及解决方案

  1. 配置重复冲突
    同时调用builder.Services.AddApplicationInsightsTelemetry()和builder.Logging.AddApplicationInsights()会导致配置冲突,前者已经自动完成日志收集的基础配置。建议简化为单一配置方式:

    public static void Main(string[] args)
    {
        var builder = WebApplication.CreateBuilder(args);
    
        ...
        
        builder.Services.AddApplicationInsightsTelemetry(options =>
        {
            options.ConnectionString = builder.Configuration["APPLICATIONINSIGHTS_CONNECTION_STRING"];
        });
        
        ... 
    
        var app = builder.Build();
    }        
    
  2. 日志级别校验
    确保自定义日志的调用级别(如LogInformation)匹配appsettings.json中Default的Information级别要求。若你的日志使用Debug级别,需调整Default或单独为自定义日志命名空间配置对应级别。

  3. 升级NuGet包版本
    当前使用的Microsoft.ApplicationInsights.AspNetCore2.21.0版本较旧,建议升级至最新稳定版,新版本修复了多处日志收集相关的已知问题。

  4. 明确日志过滤规则
    可在配置中显式指定Application Insights的日志接收级别,避免默认过滤规则拦截:

    "Logging": {
        "LogLevel": {
            "Default": "Information",
            "Microsoft.AspNetCore": "Warning"
        },
        "ApplicationInsights": {
            "LogLevel": {
                "Default": "Information"
            }
        }
    }
    
  5. 验证连接字符串有效性
    可在本地appsettings.json中直接写入连接字符串,测试本地运行时日志是否能正常发送到Application Insights,排除Azure环境变量配置的潜在问题。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.20 04:28:18