How to get detailed log information from SignalR.SelfHost?

Joachim Schei 0 Reputation points
2023-04-21T11:38:12.7033333+00:00

Hi everybody, Our SignalR.Selfhost crashes in a certain situation with a very unspecific System.Net.WebSockets.WebSocketException (System.Net.WebSockets.WebSocketException (0x80004005): Ein interner WebSocket-Fehler ist aufgetreten. Weitere Informationen können Sie, sofern vorhanden, der InnerException entnehmen. ---> System.Net.Sockets.SocketException (0x80004005): Eine vorhandene Verbindung wurde vom Remotehost geschlossen) but I don´t understand why. Some detailed log information from the hub would be useful. How do I retrieve this information? Thanks in advance Jo

Azure SignalR Service
Azure SignalR Service
An Azure service that is used for adding real-time communications to web applications.
123 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. brtrach-MSFT 15,356 Reputation points Microsoft Employee
    2023-04-22T03:34:29.3633333+00:00

    Hello Jo, to retrieve detailed log information from SignalR.SelfHost, you can enable server-side logging by using ConfigureLogging. Here is a sample usage:

    using Microsoft.AspNetCore.SignalR;
    using Microsoft.Extensions.Logging;
    
    public class MyHub : Hub
    {
        private readonly ILogger<MyHub> _logger;
    
        public MyHub(ILogger<MyHub> logger)
        {
            _logger = logger;
        }
    
        public async Task SendMessage(string user, string message)
        {
            _logger.LogInformation($"Received message from {user}: {message}");
            await Clients.All.SendAsync("ReceiveMessage", user, message);
        }
    }
    
    

    You can then configure logging in your Startup.cs file:

    public class Startup
    {
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddSignalR();
            services.AddLogging();
        }
    
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory)
        {
            loggerFactory.AddFile("Logs/myapp-{Date}.txt");
    
            app.UseRouting();
            app.UseEndpoints(endpoints =>
            {
                endpoints.MapHub<MyHub>("/myhub");
            });
        }
    }
    
    

    This will log information to a file named myapp-{Date}.txt in the Logs folder. You can customize the logging behavior by modifying the loggerFactory object. I hope this helps! Let me know if you have any further questions.

    0 comments No comments