Rename to reflect use with game servers. Filter messages on server log page by the currently opened server.

This commit is contained in:
Pat Hartl 2023-04-14 01:31:04 -05:00
parent e8456de2cb
commit 9fae96bc9e
6 changed files with 23 additions and 21 deletions

View file

@ -1,12 +1,13 @@
using Microsoft.AspNetCore.SignalR; using Microsoft.AspNetCore.SignalR;
using NLog;
namespace LANCommander.Hubs 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);
} }
} }
} }

View file

@ -1,23 +1,24 @@
using Microsoft.AspNetCore.SignalR.Client; using Microsoft.AspNetCore.SignalR.Client;
using NLog;
namespace LANCommander.Logging namespace LANCommander.Logging
{ {
public class LoggingHubConnection : IAsyncDisposable public class GameServerHubConnection : IAsyncDisposable
{ {
private HubConnection? HubConnection; private HubConnection? HubConnection;
private string HubUrl; private string HubUrl;
public LoggingHubConnection(string hubUrl) public GameServerHubConnection(string hubUrl)
{ {
HubUrl = hubUrl; HubUrl = hubUrl;
} }
public async Task Log(string logMessage) public async Task Log(Guid serverId, string message)
{ {
await EnsureConnection(); await EnsureConnection();
if (HubConnection != null) if (HubConnection != null)
await HubConnection.SendAsync("Log", logMessage); await HubConnection.SendAsync("Log", serverId, message);
} }
public async Task EnsureConnection() public async Task EnsureConnection()

View file

@ -4,25 +4,23 @@ using NLog.Targets;
namespace LANCommander.Logging namespace LANCommander.Logging
{ {
[Target("LoggingHub")] [Target("GameServerHub")]
public class LoggingHubTarget : AsyncTaskTarget public class GameServerHubTarget : AsyncTaskTarget
{ {
private LoggingHubConnection? Connection; private GameServerHubConnection? Connection;
[RequiredParameter] [RequiredParameter]
public string HubUrl { get; set; } public string HubUrl { get; set; }
protected override void InitializeTarget() protected override void InitializeTarget()
{ {
Connection = new LoggingHubConnection(HubUrl); Connection = new GameServerHubConnection(HubUrl);
} }
protected override async Task WriteAsyncTask(LogEventInfo logEvent, CancellationToken token) protected override async Task WriteAsyncTask(LogEventInfo logEvent, CancellationToken token)
{ {
string message = Layout.Render(logEvent); if (Connection != null && logEvent.Properties.ContainsKey("ServerId"))
await Connection.Log((Guid)logEvent.Properties["ServerId"], logEvent.FormattedMessage);
if (Connection != null)
await Connection.Log(message);
} }
protected override async void CloseTarget() protected override async void CloseTarget()

View file

@ -1,5 +1,6 @@
@page "/Servers/{id:guid}/Logs" @page "/Servers/{id:guid}/Logs"
@using Microsoft.AspNetCore.SignalR.Client @using Microsoft.AspNetCore.SignalR.Client
@using NLog;
@using XtermBlazor @using XtermBlazor
@attribute [Authorize] @attribute [Authorize]
@inject ServerService ServerService @inject ServerService ServerService
@ -23,11 +24,12 @@
protected override async Task OnInitializedAsync() protected override async Task OnInitializedAsync()
{ {
HubConnection = new HubConnectionBuilder() HubConnection = new HubConnectionBuilder()
.WithUrl(NavigationManager.ToAbsoluteUri("/hubs/logging")) .WithUrl(NavigationManager.ToAbsoluteUri("/hubs/gameserver"))
.Build(); .Build();
HubConnection.On<string>("Log", (message) => HubConnection.On<Guid, string>("Log", (serverId, message) =>
{ {
if (serverId == Id)
Terminal.WriteLine(message); Terminal.WriteLine(message);
}); });

View file

@ -144,7 +144,7 @@ app.UseAuthorization();
app.UseMvcWithDefaultRoute(); app.UseMvcWithDefaultRoute();
app.MapHub<LoggingHub>("/hubs/logging"); app.MapHub<GameServerHub>("/hubs/gameserver");
app.UseEndpoints(endpoints => app.UseEndpoints(endpoints =>
{ {

View file

@ -16,13 +16,13 @@
<!-- File Target for all log messages with basic details --> <!-- File Target for all log messages with basic details -->
<target xsi:type="File" name="MainLogFile" fileName="${basedir}/Logs/log.txt" archiveEvery="Day" /> <target xsi:type="File" name="MainLogFile" fileName="${basedir}/Logs/log.txt" archiveEvery="Day" />
<target xsi:type="File" name="GameServerLogFile" fileName="${basedir}/Logs/GameServers/${event-properties:ServerName}.log" archiveEvery="Day" /> <target xsi:type="File" name="GameServerLogFile" fileName="${basedir}/Logs/GameServers/${event-properties:ServerName}.log" archiveEvery="Day" />
<target xsi:type="LoggingHub" name="GameServerHub" hubUrl="http://localhost:1337/hubs/logging" /> <target xsi:type="GameServerHub" name="GameServerHub" hubUrl="http://localhost:1337/hubs/gameserver" />
</targets> </targets>
<!-- rules to map from logger name to target --> <!-- rules to map from logger name to target -->
<rules> <rules>
<!--All logs, including from Microsoft--> <!--All logs, including from Microsoft-->
<logger name="*" minlevel="Trace" writeTo="MainLogFile,LoggingHub" /> <logger name="*" minlevel="Trace" writeTo="MainLogFile" />
<logger name="LANCommander.Services.ServerProcessService" minlevel="Info" writeTo="GameServerLogFile,GameServerHub" /> <logger name="LANCommander.Services.ServerProcessService" minlevel="Info" writeTo="GameServerLogFile,GameServerHub" />
<!--Skip non-critical Microsoft logs and so log only own logs (BlackHole) --> <!--Skip non-critical Microsoft logs and so log only own logs (BlackHole) -->
<!--<logger name="Microsoft.*" maxlevel="Info" final="true" /> <!--<logger name="Microsoft.*" maxlevel="Info" final="true" />