我已经在 azure 门户上部署了 signalrserver。它接收并发送回针对连接的客户端的消息。问题是我没有看到
ILogger
的任何日志。可能是什么原因?请注意,我启用了日志 App Service log
。
我正在查看 Azure 门户的
Log stream
部分,但没有显示我的 ILogger _logger
消息,我也通过 cli 检查过:
az webapp log tail --name MySignalRApp1 --resource-group MyResourceGroup
另请注意,除了
Console.Writeline
之外,我还尝试过基本的 ILogger
,而且它也不会在 azure 上打印任何日志。
我错过了什么?
程序.cs:
using Microsoft.Extensions.Logging;
using Microsoft.ApplicationInsights.Extensibility;
var builder = WebApplication.CreateBuilder(args);
builder.Logging.ClearProviders();
builder.Services.AddLogging();
builder.Services.AddApplicationInsightsTelemetry(options =>
{
options.ConnectionString = builder.Configuration["ApplicationInsights:ConnectionString"];
});
builder.Logging.AddConsole();
builder.Logging.AddApplicationInsights();
builder.Services.AddSignalR().AddAzureSignalR(options =>
{
options.ConnectionString = builder.Configuration["AzureSignalRConnectionString"];
});
var app = builder.Build();
app.UseRouting();
app.UseStaticFiles();
app.MapHub<ChatHub>("/chathub");
app.Run();
ChatHub:(缩短)
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading.Tasks;
public class ChatHub : Hub
{
private readonly ILogger<ChatHub> _logger;
public ChatHub(ILogger<ChatHub> logger)
{
_logger = logger;
}
public async Task Connect(string username)
{
_logger.LogInformation($"Test_1");
_logger.LogDebug($"Test_2");
_logger.LogTrace($"Test_3");
}
}
我使用下面的代码在 Azure Application Insights 中记录
Ilogger
和 SignalR service
相关日志。
程序.cs:
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddSignalR().AddAzureSignalR(
options =>
{
options.ConnectionString = builder.Configuration["AzureSignalRConnectionString"];
});
builder.Logging.AddApplicationInsights(
configureTelemetryConfiguration: (config) =>
config.ConnectionString = builder.Configuration["APPLICATIONINSIGHTS_CONNECTION_STRING"],
configureApplicationInsightsLoggerOptions: (options) => { }
);
var app = builder.Build();
app.UseDefaultFiles();
app.UseRouting();
app.UseStaticFiles();
app.MapHub<ChatSampleHub>("/chat");
app.Run();
您可以在单独的方法中添加 Ilogger 消息。 ChatSampleHub.cs:
public class ChatSampleHub : Hub
{
private readonly ILogger<ChatSampleHub> _logger;
public ChatSampleHub(ILogger<ChatSampleHub> logger)
{
_logger = logger;
_logger.LogInformation("Test_1");
_logger.LogDebug("Test_2");
_logger.LogTrace("Test_3");
}
public Task BroadcastMessage(string name, string message) =>
Clients.All.SendAsync("broadcastMessage", name, message);
public Task Echo(string name, string message) =>
Clients.Client(Context.ConnectionId)
.SendAsync("echo", name, $"{message} (echo from server)");
}
appsettings.json:
{
"Logging": {
"LogLevel": {
"Default": "Trace",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*",
"AzureSignalRConnectionString": "Endpoint=https://signalR.service.signalr.net;AccessKey=9UF4GqEanyXXXQt1;Version=1.0;",
"APPLICATIONINSIGHTS_CONNECTION_STRING": "InstrumentationKey=f8acXXXX"
}
控制台日志:
info: Microsoft.Hosting.Lifetime[14]
Now listening on: https://localhost:7098
info: Microsoft.Hosting.Lifetime[14]
Now listening on: http://localhost:5114
info: Microsoft.Hosting.Lifetime[0]
Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
Hosting environment: Development
info: Microsoft.Hosting.Lifetime[0]
Content root path: C:\Users\uname\Source\Repos\WebApplication3
info: Microsoft.Azure.SignalR.ServiceConnection[20]
Service connection 3eb723e9-XX225-b93a-507b6eb78b72 connected.
info: Microsoft.Azure.SignalR.ServiceConnection[20]
Service connection 776f3fab-44XX-a760-c49377e5c382 connected.
info: Microsoft.Azure.SignalR.ServiceConnection[20]
Service connection 5cab944XX84-4eeb-8ee4-df866b25ae39 connected.
info: Microsoft.Azure.SignalR.ServiceConnection[20]
Service connection fc56ebda-88e2-465c-99c4-b77f4a6f7dd1 connected.
info: Microsoft.Azure.SignalR.ServiceConnection[20]
Service connection d87b9b26-da4b-438b-a473-fbffced9782f connected.
info: Microsoft.Azure.SignalR.StrongServiceConnectionContainer[1]
Hub 'ChatSampleHub' is now connected to '(Primary)https://signalR.service.signalr.net(hub=ChatSampleHub)'.
info: Microsoft.Azure.SignalR.NegotiateHandler[0]
Use default handshake timeout.
info: ChatSampleHub[0]
Test_1
info: ChatSampleHub[0]
Test_1
info: Microsoft.Azure.SignalR.ServiceLifetimeManager[0]
Start to broadcast message 543904857XX70528.
info: Microsoft.Azure.SignalR.MultiEndpointMessageWriter[11]
Route message 54390XX0070528 to service endpoint (Primary)https://signalR.service.signalr.net(hub=ChatSampleHub).
info: Microsoft.Azure.SignalR.ServiceLifetimeManager[110]
Succeeded to send message 543904XX0070528.
应用见解:
App Service=>Monitoring=>Application Insights=>Investigate=>Transaction Search
下查看日志:您还可以在
Application Insights=>Overview=>Logs
下查看日志:
您可以在本地调试代码并检查是否命中了 Ilogger 代码ChatHub 类中的 Connect 方法。