Reputation: 3573
I've got signalrserver deployed on azure portal. It receives and sends back messages against connected clients. The problem is i do not see any logs out of ILogger
. What could be the reason? Note that I enabled logs in App Service log
.
I was looking at azure portal at Log stream
section and none of my ILogger _logger
msgs are shown, i have also checked via cli:
az webapp log tail --name MySignalRApp1 --resource-group MyResourceGroup
Please also note that that ive tried with basic Console.Writeline
besides ILogger
and it also doesn't print out any logs on azure.
What have i missed?
Program.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: (shortened)
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");
}
}
Upvotes: 0
Views: 55
Reputation: 8579
I have used below code to log the Ilogger
and SignalR service
related logs in Azure Application Insights.
Program.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();
You can add the Ilogger messages in a separate method. 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"
}
Console logs:
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=>App Service Logs
Application Insights:
App Service=>Monitoring=>Application Insights=>Investigate=>Transaction Search
:You can also check the logs under Application Insights=>Overview=>Logs
:
You can debug the code locally and check if you are hitting the Ilogger code Connect method in your ChatHub class.
Upvotes: 0