Skip to content

Commit

Permalink
Add server diagnostics log
Browse files Browse the repository at this point in the history
  • Loading branch information
electroly committed Dec 2, 2024
1 parent c880941 commit 3197783
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 10 deletions.
42 changes: 38 additions & 4 deletions src/J.App/Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,21 @@ namespace J.App;

public sealed class Client(IHttpClientFactory httpClientFactory, Preferences preferences) : IDisposable
{
private const int MAX_LOG_LINES = 10_000;
private readonly HttpClient _httpClient = httpClientFactory.CreateClient(typeof(Client).FullName!);

private readonly Lock _lock = new();
private readonly Lock _processLock = new();
private Process? _process;

private readonly Lock _logLock = new();
private readonly Queue<string> _log = [];

public int Port { get; private set; } = -1;
public string SessionPassword { get; } = Guid.NewGuid().ToString();

public void Start()
{
lock (_lock)
lock (_processLock)
{
if (_process is not null)
throw new InvalidOperationException("The web server is already running.");
Expand All @@ -35,6 +39,8 @@ public void Start()
WorkingDirectory = dir,
UseShellExecute = false,
CreateNoWindow = false,
RedirectStandardError = true,
RedirectStandardOutput = true,
};

Port = FindRandomUnusedPort();
Expand All @@ -46,18 +52,46 @@ public void Start()
psi.Environment["JACKPOT_SESSION_PASSWORD"] = SessionPassword;

_process = Process.Start(psi)!;

_process.OutputDataReceived += Process_DataReceived;
_process.BeginOutputReadLine();
_process.ErrorDataReceived += Process_DataReceived;
_process.BeginErrorReadLine();

ApplicationSubProcesses.Add(_process);
}
}

private void Process_DataReceived(object sender, DataReceivedEventArgs e)
{
if (string.IsNullOrWhiteSpace(e.Data))
return;

lock (_logLock)
{
while (_log.Count >= MAX_LOG_LINES)
_log.Dequeue();

_log.Enqueue(e.Data!);
}
}

public List<string> GetLog()
{
lock (_logLock)
{
return new(_log);
}
}

public void Dispose()
{
Stop();
}

public void Stop()
{
lock (_lock)
lock (_processLock)
{
if (_process is not null)
{
Expand All @@ -70,7 +104,7 @@ public void Stop()

public void Restart()
{
lock (_lock)
lock (_processLock)
{
Stop();
Start();
Expand Down
14 changes: 12 additions & 2 deletions src/J.App/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public sealed partial class MainForm : Form
private readonly M3u8FolderSync _m3U8FolderSync;
private readonly ImportControl _importControl;
private readonly TagsControl _tagsControl;
private readonly ProcessTempDir _processTempDir;
private readonly Ui _ui;
private readonly ToolStrip _toolStrip;
private readonly ToolStripDropDownButton _filterButton,
Expand Down Expand Up @@ -78,7 +79,8 @@ public MainForm(
MovieExporter movieExporter,
M3u8FolderSync m3U8FolderSync,
ImportControl importControl,
TagsControl tagsControl
TagsControl tagsControl,
ProcessTempDir processTempDir
)
{
_serviceProvider = serviceProvider;
Expand All @@ -90,6 +92,7 @@ TagsControl tagsControl
_m3U8FolderSync = m3U8FolderSync;
_importControl = importControl;
_tagsControl = tagsControl;
_processTempDir = processTempDir;
Ui ui = new(this);
_ui = ui;

Expand Down Expand Up @@ -461,11 +464,18 @@ private void AboutButton_Click(object? sender, EventArgs e)
Icon = TaskDialogIcon.Information,
Text = $"Version {version}",
};
taskDialogPage.Buttons.Add("Server log");
taskDialogPage.Buttons.Add("License info");
taskDialogPage.Buttons.Add(TaskDialogButton.OK);
taskDialogPage.DefaultButton = taskDialogPage.Buttons[1];
taskDialogPage.DefaultButton = taskDialogPage.Buttons[2];
var clicked = TaskDialog.ShowDialog(this, taskDialogPage);
if (clicked == taskDialogPage.Buttons[0])
{
var filePath = Path.Combine(_processTempDir.Path, "server.log");
File.WriteAllLines(filePath, _client.GetLog());
Process.Start("notepad.exe", filePath);
}
else if (clicked == taskDialogPage.Buttons[1])
{
Process
.Start(
Expand Down
6 changes: 6 additions & 0 deletions src/J.App/Resources/License.html
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,12 @@ <h1>Jackpot Media Library &mdash; License Info</h1>
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.</pre>
</details>

<details>
<summary>Serilog</summary>
<p>The source code may be downloaded from <a href="https://github.com/serilog/serilog">GitHub</a>.
<p>Serilog is licensed under Apache License version 2.0.
</details>

<details>
<summary>SharpZipLib</summary>
<p>The source code may be downloaded from <a href="https://github.com/icsharpcode/SharpZipLib">GitHub</a>.
Expand Down
4 changes: 2 additions & 2 deletions src/J.Core/SyncMovieFileReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ CancellationToken cancel
await Parallel
.ForEachAsync(
keys,
new ParallelOptions { MaxDegreeOfParallelism = 32, CancellationToken = cancel },
new ParallelOptions { MaxDegreeOfParallelism = 64, CancellationToken = cancel },
async (key, cancel) =>
{
var zipIndex = await ReadZipIndexAsync(key, cancel).ConfigureAwait(false);
Expand Down Expand Up @@ -69,7 +69,7 @@ await Parallel
await Parallel
.ForEachAsync(
entriesToDownload,
new ParallelOptions { MaxDegreeOfParallelism = 32, CancellationToken = cancel },
new ParallelOptions { MaxDegreeOfParallelism = 64, CancellationToken = cancel },
async (x, cancel) =>
{
var zipIndex = zipIndices[x.Key];
Expand Down
6 changes: 6 additions & 0 deletions src/J.Server/J.Server.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@
<!-- Allow access to System.Security.Cryptography.ProtectedData -->
<FrameworkReference Include="Microsoft.WindowsDesktop.App" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Serilog" Version="4.1.0" />
<PackageReference Include="Serilog.AspNetCore" Version="8.0.3" />
<PackageReference Include="Serilog.Sinks.Console" Version="6.0.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\J.Core\J.Core.csproj" />
Expand Down
6 changes: 4 additions & 2 deletions src/J.Server/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,19 @@
using J.Core.Data;
using J.Server;
using Microsoft.AspNetCore.Mvc;
using Serilog;

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddCore();
builder.Services.AddHttpLogging(o => { });
builder.Services.AddTransient<ServerMovieFileReader>();
builder.Services.AddSingleton<IAmazonS3>(services =>
services.GetRequiredService<AccountSettingsProvider>().CreateAmazonS3Client()
);

builder.Host.UseSerilog((context, configuration) => configuration.ReadFrom.Configuration(context.Configuration));

var app = builder.Build();
app.UseHttpLogging();
app.UseSerilogRequestLogging();

var configuredPort = GetPortNumber();
var configuredSessionPassword = Environment.GetEnvironmentVariable("JACKPOT_SESSION_PASSWORD") ?? "";
Expand Down
10 changes: 10 additions & 0 deletions src/J.Server/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,15 @@
"Microsoft.AspNetCore.HttpLogging.HttpLoggingMiddleware": "Information"
}
},
"Serilog": {
"Using": [ "Serilog.Sinks.Console" ],
"MinimumLevel": "Debug",
"WriteTo": [
{
"Name": "Console"
}
],
"Enrich": [ "FromLogContext" ]
},
"AllowedHosts": "*"
}

0 comments on commit 3197783

Please sign in to comment.