SignalR is part of the .NET Core framework that allows for real-time communication between a server and any number of clients. The traditional example is a JavaScript client that can receive updates directly from a web server asynchronously, but you can also connect to a SignalR hub with a C# client.
HubConnectionBuilder
To create a SignalR connection in a C# client, you use the HubConnectionBuilder:
…But now it isn’t working
So in a previous post I talked about turning on SignalR logging at the server to see if something goofy is happening, but what if you wanted to turn on logging on the client end? Well, HubConnectionBuilder has a ConfigureLogging too, just like a WebApplication:
…But this doesn’t really do anything. Even though we tuned the logging level to Debug, there is no logging provider defined, so the log messages just disappear into the ether.
Tying in an existing logger
I wasn’t able to find a definitive way for hooking an existing ILoggerProvider in to the SignalR client, and, if I do, I’ll either update this post or write a new one. In the meantime, I came up with this way, which does work:
Create Extension Method on ILogger
Add the logger as an ILoggerProvider
…And now you can figure out that the reason your SignalR application isn’t working is because you forgot to add a parameterless constructor to your DTO and the new JSON serializer barfed on it, but didn’t bother telling you until you went through all of this to actually be able to see the logs.