From 9fae96bc9edf1c38d61882d65c01ddaf29efc550 Mon Sep 17 00:00:00 2001 From: Pat Hartl Date: Fri, 14 Apr 2023 01:31:04 -0500 Subject: [PATCH] Rename to reflect use with game servers. Filter messages on server log page by the currently opened server. --- LANCommander/Hubs/LoggingHub.cs | 7 ++++--- ...HubConnection.cs => GameServerHubConnection.cs} | 9 +++++---- ...{LoggingHubTarget.cs => GameServerHubTarget.cs} | 14 ++++++-------- LANCommander/Pages/Servers/Logs.razor | 8 +++++--- LANCommander/Program.cs | 2 +- LANCommander/nlog.config | 4 ++-- 6 files changed, 23 insertions(+), 21 deletions(-) rename LANCommander/Logging/{LoggingHubConnection.cs => GameServerHubConnection.cs} (83%) rename LANCommander/Logging/{LoggingHubTarget.cs => GameServerHubTarget.cs} (60%) diff --git a/LANCommander/Hubs/LoggingHub.cs b/LANCommander/Hubs/LoggingHub.cs index bf0a489..797dd04 100644 --- a/LANCommander/Hubs/LoggingHub.cs +++ b/LANCommander/Hubs/LoggingHub.cs @@ -1,12 +1,13 @@ using Microsoft.AspNetCore.SignalR; +using NLog; namespace LANCommander.Hubs { - public class LoggingHub : Hub + public class GameServerHub : Hub { - public void Log(string logMessage) + public void Log(Guid serverId, string message) { - Clients.All.SendAsync("Log", logMessage); + Clients.All.SendAsync("Log", serverId, message); } } } diff --git a/LANCommander/Logging/LoggingHubConnection.cs b/LANCommander/Logging/GameServerHubConnection.cs similarity index 83% rename from LANCommander/Logging/LoggingHubConnection.cs rename to LANCommander/Logging/GameServerHubConnection.cs index 087b203..a01155e 100644 --- a/LANCommander/Logging/LoggingHubConnection.cs +++ b/LANCommander/Logging/GameServerHubConnection.cs @@ -1,23 +1,24 @@ using Microsoft.AspNetCore.SignalR.Client; +using NLog; namespace LANCommander.Logging { - public class LoggingHubConnection : IAsyncDisposable + public class GameServerHubConnection : IAsyncDisposable { private HubConnection? HubConnection; private string HubUrl; - public LoggingHubConnection(string hubUrl) + public GameServerHubConnection(string hubUrl) { HubUrl = hubUrl; } - public async Task Log(string logMessage) + public async Task Log(Guid serverId, string message) { await EnsureConnection(); if (HubConnection != null) - await HubConnection.SendAsync("Log", logMessage); + await HubConnection.SendAsync("Log", serverId, message); } public async Task EnsureConnection() diff --git a/LANCommander/Logging/LoggingHubTarget.cs b/LANCommander/Logging/GameServerHubTarget.cs similarity index 60% rename from LANCommander/Logging/LoggingHubTarget.cs rename to LANCommander/Logging/GameServerHubTarget.cs index 4061be6..33d4114 100644 --- a/LANCommander/Logging/LoggingHubTarget.cs +++ b/LANCommander/Logging/GameServerHubTarget.cs @@ -4,25 +4,23 @@ using NLog.Targets; namespace LANCommander.Logging { - [Target("LoggingHub")] - public class LoggingHubTarget : AsyncTaskTarget + [Target("GameServerHub")] + public class GameServerHubTarget : AsyncTaskTarget { - private LoggingHubConnection? Connection; + private GameServerHubConnection? Connection; [RequiredParameter] public string HubUrl { get; set; } protected override void InitializeTarget() { - Connection = new LoggingHubConnection(HubUrl); + Connection = new GameServerHubConnection(HubUrl); } protected override async Task WriteAsyncTask(LogEventInfo logEvent, CancellationToken token) { - string message = Layout.Render(logEvent); - - if (Connection != null) - await Connection.Log(message); + if (Connection != null && logEvent.Properties.ContainsKey("ServerId")) + await Connection.Log((Guid)logEvent.Properties["ServerId"], logEvent.FormattedMessage); } protected override async void CloseTarget() diff --git a/LANCommander/Pages/Servers/Logs.razor b/LANCommander/Pages/Servers/Logs.razor index c4feac7..8a02cc4 100644 --- a/LANCommander/Pages/Servers/Logs.razor +++ b/LANCommander/Pages/Servers/Logs.razor @@ -1,5 +1,6 @@ @page "/Servers/{id:guid}/Logs" @using Microsoft.AspNetCore.SignalR.Client +@using NLog; @using XtermBlazor @attribute [Authorize] @inject ServerService ServerService @@ -23,12 +24,13 @@ protected override async Task OnInitializedAsync() { HubConnection = new HubConnectionBuilder() - .WithUrl(NavigationManager.ToAbsoluteUri("/hubs/logging")) + .WithUrl(NavigationManager.ToAbsoluteUri("/hubs/gameserver")) .Build(); - HubConnection.On("Log", (message) => + HubConnection.On("Log", (serverId, message) => { - Terminal.WriteLine(message); + if (serverId == Id) + Terminal.WriteLine(message); }); await HubConnection.StartAsync(); diff --git a/LANCommander/Program.cs b/LANCommander/Program.cs index 149bd1a..a705a6f 100644 --- a/LANCommander/Program.cs +++ b/LANCommander/Program.cs @@ -144,7 +144,7 @@ app.UseAuthorization(); app.UseMvcWithDefaultRoute(); -app.MapHub("/hubs/logging"); +app.MapHub("/hubs/gameserver"); app.UseEndpoints(endpoints => { diff --git a/LANCommander/nlog.config b/LANCommander/nlog.config index 06075b0..c0f6dea 100644 --- a/LANCommander/nlog.config +++ b/LANCommander/nlog.config @@ -16,13 +16,13 @@ - + - +