+
@@ -63,6 +64,11 @@
NavigationManager.NavigateTo($"/Servers/{server.Id}/Edit");
}
+ private void Logs(Server server)
+ {
+ NavigationManager.NavigateTo($"/Servers/{server.Id}/Logs");
+ }
+
private void Start(Server server)
{
ServerProcessService.StartServer(server);
diff --git a/LANCommander/Pages/Servers/Logs.razor b/LANCommander/Pages/Servers/Logs.razor
new file mode 100644
index 0000000..223e207
--- /dev/null
+++ b/LANCommander/Pages/Servers/Logs.razor
@@ -0,0 +1,42 @@
+@page "/Servers/{id:guid}/Logs"
+@using Microsoft.AspNetCore.SignalR.Client
+@attribute [Authorize]
+@inject ServerService ServerService
+@inject ServerProcessService ServerProcessService
+@inject NavigationManager NavigationManager
+@implements IAsyncDisposable
+
+
+ @foreach (var message in Messages)
+ {
+ @message
+ }
+
+
+@code {
+ [Parameter] public Guid Id { get; set; }
+
+ HubConnection? HubConnection;
+ List Messages = new List();
+
+ protected override async Task OnInitializedAsync()
+ {
+ HubConnection = new HubConnectionBuilder()
+ .WithUrl(NavigationManager.ToAbsoluteUri("/hubs/logging"))
+ .Build();
+
+ HubConnection.On("Log", (message) =>
+ {
+ Messages.Add(message);
+ InvokeAsync(StateHasChanged);
+ });
+
+ await HubConnection.StartAsync();
+ }
+
+ public async ValueTask DisposeAsync()
+ {
+ if (HubConnection is not null)
+ await HubConnection.DisposeAsync();
+ }
+}
diff --git a/LANCommander/Program.cs b/LANCommander/Program.cs
index 7fe2541..149bd1a 100644
--- a/LANCommander/Program.cs
+++ b/LANCommander/Program.cs
@@ -1,12 +1,14 @@
using BeaconLib;
using LANCommander.Data;
using LANCommander.Data.Models;
+using LANCommander.Hubs;
using LANCommander.Services;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.IdentityModel.Tokens;
+using NLog.Web;
using System.Text;
var builder = WebApplication.CreateBuilder(args);
@@ -114,6 +116,8 @@ builder.WebHost.UseKestrel(options =>
options.Limits.MaxRequestBodySize = 1024 * 1024 * 150;
});
+builder.Host.UseNLog();
+
var app = builder.Build();
// Configure the HTTP request pipeline.
@@ -140,6 +144,8 @@ app.UseAuthorization();
app.UseMvcWithDefaultRoute();
+app.MapHub("/hubs/logging");
+
app.UseEndpoints(endpoints =>
{
endpoints.MapBlazorHub();
diff --git a/LANCommander/Services/ServerProcessService.cs b/LANCommander/Services/ServerProcessService.cs
index 136a489..0cb6f2c 100644
--- a/LANCommander/Services/ServerProcessService.cs
+++ b/LANCommander/Services/ServerProcessService.cs
@@ -1,10 +1,13 @@
using LANCommander.Data.Models;
+using NLog;
using System.Diagnostics;
namespace LANCommander.Services
{
public class ServerProcessService
{
+ private readonly Logger Logger = LogManager.GetCurrentClassLogger();
+
public Dictionary Processes = new Dictionary();
public Dictionary Threads { get; set; } = new Dictionary();
@@ -15,19 +18,40 @@ namespace LANCommander.Services
process.StartInfo.FileName = server.Path;
process.StartInfo.WorkingDirectory = server.WorkingDirectory;
process.StartInfo.Arguments = server.Arguments;
- process.StartInfo.UseShellExecute = true;
+ process.StartInfo.UseShellExecute = false;
+ process.StartInfo.RedirectStandardOutput = true;
+ process.StartInfo.RedirectStandardError = true;
+ process.EnableRaisingEvents = true;
+
+ process.OutputDataReceived += new DataReceivedEventHandler((sender, e) =>
+ {
+ Logger.Info("Game Server {ServerName} ({ServerId}) Info: {Message}", server.Name, server.Id, e.Data);
+ });
+
+ process.ErrorDataReceived += new DataReceivedEventHandler((sender, e) =>
+ {
+ Logger.Error("Game Server {ServerName} ({ServerId}) Error: {Message}", server.Name, server.Id, e.Data);
+ });
process.Start();
+ process.BeginErrorReadLine();
+ process.BeginOutputReadLine();
+
Processes[server.Id] = process;
await process.WaitForExitAsync();
}
+
public void StopServer(Server server)
{
if (Processes.ContainsKey(server.Id))
- Processes[server.Id].Kill();
+ {
+ var process = Processes[server.Id];
+
+ process.Kill();
+ }
}
}
}
diff --git a/LANCommander/nlog.config b/LANCommander/nlog.config
new file mode 100644
index 0000000..06075b0
--- /dev/null
+++ b/LANCommander/nlog.config
@@ -0,0 +1,33 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file