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

Azure部署的SignalR服务ILogger日志不显示问题求助

问题排查与解决方案

核心问题分析

你的SignalR服务能正常收发消息,但ILogger和Console.WriteLine的日志无法在Azure App Service的Log Stream或az webapp log tail中显示,主要原因是日志提供者配置缺失、日志级别未正确设置或Azure门户日志配置未生效。

具体修复步骤

1. 修正Program.cs中的日志提供者配置

你调用builder.Logging.ClearProviders()移除了所有默认日志提供者,其中包含Azure App Service专用的日志输出组件,导致日志无法传递到Log Stream。需调整配置,添加Azure Web App诊断日志提供者,并绑定配置文件的日志规则。

修改后的Program.cs关键代码:

var builder = WebApplication.CreateBuilder(args);

// 若无需清空所有默认提供者,可删除ClearProviders调用;若必须清空,需后续手动添加Azure所需组件
// builder.Logging.ClearProviders();

// 绑定配置文件中的日志级别设置
builder.Logging.AddConfiguration(builder.Configuration.GetSection("Logging"));

builder.Services.AddApplicationInsightsTelemetry(options =>
{
    options.ConnectionString = builder.Configuration["ApplicationInsights:ConnectionString"];
});

// 添加Console日志提供者 + Azure App Service诊断日志提供者(核心配置)
builder.Logging.AddConsole();
builder.Logging.AddAzureWebAppDiagnostics();

builder.Services.AddSignalR().AddAzureSignalR(options =>
{
    options.ConnectionString = builder.Configuration["AzureSignalRConnectionString"];
});

var app = builder.Build();
app.UseRouting();
app.UseStaticFiles();
app.MapHub<ChatHub>("/chathub"); 
app.Run();

2. 配置日志级别(appsettings.json)

默认日志过滤级别可能高于Information,导致LogInformation、LogDebug、LogTrace的输出被拦截。在appsettings.json或appsettings.Production.json中添加日志级别规则:

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "ChatHub": "Trace" // 确保ChatHub的所有日志级别都能输出
    },
    "Console": {
      "IncludeScopes": true
    }
  }
}

3. 确认Azure门户的App Service日志配置

在Azure门户进入你的App Service,依次点击监视 > 日志流,确保:

  • 已开启应用日志记录(文件系统)或应用日志记录(Blob存储)
  • 日志级别设置为信息或更低(如“详细”),确保能捕获Information及以下级别的日志

4. 验证日志输出

完成配置后重启App Service,触发Connect方法,再查看Log Stream或执行命令:

az webapp log tail --name <你的应用名> --resource-group <你的资源组>

此时应能看到Test_1、Test_2、Test_3的日志输出。

另外,也可在Application Insights的日志页面中,用以下Kusto查询验证日志是否被收集:

traces
| where message contains "Test_"
| order by timestamp desc

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.15 12:50:13