I’m working on a project where I need to pass logs from one console application to another using Azure SignalR Service. I have three components: a sender console app, a receiver console app, and an Azure Function that directly connects to SignalR. The sender app successfully connects to SignalR, but I’m not receiving any logs in the receiver app, and the Azure SignalR portal shows zero messages and outbound traffic.
Here’s the relevant code:
Sender Console App:
class Program
{
static async Task Main(string[] args)
{
var connection = new HubConnectionBuilder()
.WithUrl("<Functionapp Url>")
.Build();
await connection.StartAsync();
Log.Logger = new LoggerConfiguration()
.WriteTo.Console()
.CreateLogger();
var sender = new LogSender(connection);
string logMessage = $"Log from Sender at {DateTime.Now}";
while (true)
{
sender.SendLog(logMessage);
}
}
}
class LogSender
{
private readonly HubConnection _connection;
public LogSender(HubConnection connection)
{
_connection = connection;
}
public async void SendLog(string message)
{
Log.Information(message);
await _connection.InvokeAsync("SendLog", message);
}
}
Receiver Console App:
class Program
{
static async Task Main(string[] args)
{
var connection = new HubConnectionBuilder()
.WithUrl("<FunctionUrl>")
.Build();
connection.On<string>("ReceiveLog", log =>
{
Console.WriteLine($"Received log: {log}");
});
connection.Closed += async (exception) =>
{
Console.WriteLine($"Connection closed: {exception?.Message}");
};
try
{
await connection.StartAsync();
Console.WriteLine("Connected to SignalR hub.");
}
catch (Exception ex)
{
Console.WriteLine($"Error connecting to SignalR hub: {ex.Message}");
}
Console.WriteLine("Listening for logs. Press any key to exit.");
Console.ReadKey();
}
}
Azure Function:
public static class Function1
{
[FunctionName("negotiate")]
public static SignalRConnectionInfo Negotiate(
[HttpTrigger(AuthorizationLevel.Anonymous, "post")] HttpRequest req,
[SignalRConnectionInfo(HubName = "loghub")] SignalRConnectionInfo connectionInfo)
{
return connectionInfo;
}
}
public class loghub : ServerlessHub
{
public async Task SendLog(string message)
{
await Clients.All.SendAsync("ReceiveLog", message);
}
}
What am I missing? How can I ensure that logs are properly received in the receiver console app or any place? It seems like properly connects to Azure SignalR; when I debug the connection object, its state is ‘connected