LANCommander/LANCommander.Server/Logging/LoggingHubConnection.cs

63 lines
1.7 KiB
C#
Raw Normal View History

2024-08-04 18:44:33 -05:00
using LANCommander.Server.Services;
using Microsoft.AspNetCore.SignalR.Client;
namespace LANCommander.Logging
{
public class LoggingHubConnection : IAsyncDisposable
{
private HubConnection? HubConnection;
private string HubUrl;
public LoggingHubConnection(string hubUrl)
{
var settings = SettingService.GetSettings();
HubUrl = hubUrl.Replace("${gdc:PortNumber}", settings.Port.ToString());
}
public async Task Log(string message)
{
await EnsureConnection();
if (HubConnection != null)
await HubConnection.SendAsync("Log", message);
}
public async Task EnsureConnection()
{
if (HubConnection == null)
{
HubConnection = new HubConnectionBuilder()
.WithUrl(HubUrl)
.Build();
await HubConnection.StartAsync();
}
else if (HubConnection.State == HubConnectionState.Disconnected)
{
await HubConnection.StartAsync();
}
}
public async ValueTask DisposeAsync()
{
if (HubConnection != null)
{
try
{
await HubConnection.StopAsync();
await HubConnection.DisposeAsync();
}
catch (Exception ex)
{
// NLog.Common.InternalLogger?.LogError(ex, "Exception in LoggingHubConnection.DisposeAsync");
}
finally
{
HubConnection = null;
}
}
}
}
}