Skip to content

Commit

Permalink
Support for multiple csharpier servers running at the same time, have…
Browse files Browse the repository at this point in the history
… them write to a file based on the port. (#1329)

closes #1324

I wish I remembered what led me to this nreco package. Maybe because
serilog seemed overboard and I wanted something super lightweight.
  • Loading branch information
belav authored Aug 23, 2024
1 parent 2ed8622 commit 10e0780
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 4 deletions.
40 changes: 40 additions & 0 deletions Src/CSharpier.Cli.Tests/ServerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ namespace CSharpier.Cli.Tests;

using System.Diagnostics;
using System.Net.Http.Json;
using System.Text;
using CliWrap;
using CliWrap.EventStream;
using CSharpier.Cli.Server;
using FluentAssertions;
using NUnit.Framework;
Expand Down Expand Up @@ -67,4 +70,41 @@ public async Task Stuff()
process.Kill();
}
}

[Test]
[Ignore("leaves things running when it fails and probably won't work on GH")]
public void RunTwo()
{
var path = Path.Combine(Directory.GetCurrentDirectory(), "dotnet-csharpier.dll");

async Task NewFunction()
{
var command = Cli.Wrap("dotnet")
.WithArguments(path + " --server")
.WithValidation(CommandResultValidation.None);

using var cts = new CancellationTokenSource();
cts.CancelAfter(TimeSpan.FromSeconds(5));

await foreach (var cmdEvent in command.ListenAsync(cancellationToken: cts.Token))
{
switch (cmdEvent)
{
case StartedCommandEvent started:
Console.WriteLine($"Process started; ID: {started.ProcessId}");
break;
case StandardOutputCommandEvent stdOut:
Console.WriteLine($"Out> {stdOut.Text}");
break;
case StandardErrorCommandEvent stdErr:
throw new Exception(stdErr.Text);
case ExitedCommandEvent exited:
Console.WriteLine($"Process exited; Code: {exited.ExitCode}");
break;
}
}
}

Task.WaitAll(NewFunction(), NewFunction());
}
}
16 changes: 13 additions & 3 deletions Src/CSharpier.Cli/Server/ServerFormatter.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
namespace CSharpier.Cli.Server;

using System.Net;
using System.Net.NetworkInformation;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Hosting.Server.Features;
Expand Down Expand Up @@ -32,11 +31,21 @@ public static async Task<int> StartServer(
["Logging:File:FileSizeLimitBytes"] = "10000",
};
builder.Configuration.AddInMemoryCollection(values);
var currentPort = port ?? 0;
builder.Services.AddLogging(loggingBuilder =>
{
loggingBuilder.AddFile(
Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "server.log"),
append: true
Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "server{0}.log"),
o =>
{
// name files based on the port so that multiple processes can log without fighting over a file
// however before the server is started fully we won't have a port
// this empty error handler will make sure if two processes both try to use that initial file
// at the same time they won't crash
o.HandleFileError = _ => { };
o.FormatLogFileName = name =>
string.Format(name, currentPort == 0 ? string.Empty : currentPort);
}
);
});

Expand All @@ -50,6 +59,7 @@ var address in (app as IApplicationBuilder)
)
{
var uri = new Uri(address);
currentPort = uri.Port;
logger.LogInformation("Started on " + uri.Port);
}
});
Expand Down
2 changes: 1 addition & 1 deletion Src/CSharpier/DebugLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ internal class DebugLogger
private static object lockObject = new();

[Conditional("DEBUG")]
public static void Log(string message)
public static void Log(object message)
{
lock (lockObject)
{
Expand Down

0 comments on commit 10e0780

Please sign in to comment.