Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
8 changes: 4 additions & 4 deletions src/SeqCli/Cli/Commands/Forwarder/InstallCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
using System.Security.AccessControl;
using System.ServiceProcess;
using System.Threading.Tasks;
using Seq.Forwarder.Cli.Features;
using Seq.Forwarder.Util;
using SeqCli.Forwarder.Cli.Features;
using SeqCli.Forwarder.Util;
using SeqCli;
using SeqCli.Cli;
using SeqCli.Cli.Features;
Expand All @@ -33,7 +33,7 @@

// ReSharper disable once ClassNeverInstantiated.Global

namespace Seq.Forwarder.Cli.Commands
namespace SeqCli.Forwarder.Cli.Commands
{
[Command("forwarder", "install", "Install the forwarder as a Windows service")]
[SuppressMessage("Interoperability", "CA1416:Validate platform compatibility")]
Expand Down Expand Up @@ -95,7 +95,7 @@ int Setup()
ServiceController controller;
try
{
Console.WriteLine("Checking the status of the Seq Forwarder service...");
Console.WriteLine($"Checking the status of the {SeqCliForwarderWindowsService.WindowsServiceName} service...");

controller = new ServiceController(SeqCliForwarderWindowsService.WindowsServiceName);
Console.WriteLine("Status is {0}", controller.Status);
Expand Down
2 changes: 1 addition & 1 deletion src/SeqCli/Cli/Commands/Forwarder/RestartCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

// ReSharper disable UnusedType.Global

namespace Seq.Forwarder.Cli.Commands
namespace SeqCli.Forwarder.Cli.Commands
{
[Command("forwarder", "restart", "Restart the forwarder Windows service")]
[SuppressMessage("Interoperability", "CA1416:Validate platform compatibility")]
Expand Down
64 changes: 43 additions & 21 deletions src/SeqCli/Cli/Commands/Forwarder/RunCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,11 @@
// limitations under the License.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Net;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
using Autofac;
using Autofac.Extensions.DependencyInjection;
Expand All @@ -30,13 +29,13 @@
using SeqCli.Config.Forwarder;
using SeqCli.Forwarder;
using SeqCli.Forwarder.Util;
using SeqCli.Forwarder.Web;
using SeqCli.Forwarder.Web.Api;
using SeqCli.Forwarder.Web.Host;
using Serilog;
using Serilog.Core;
using Serilog.Events;
using Serilog.Formatting.Compact;
using Serilog.Formatting.Display;

#if WINDOWS
using SeqCli.Forwarder.ServiceProcess;
Expand Down Expand Up @@ -79,6 +78,7 @@ protected override async Task<int> Run(string[] unrecognized)

try
{
// ISSUE: we can't really rely on the default `SeqCliConfig` path being readable when running as a service.
config = SeqCliConfig.Read(); // _storagePath.ConfigFilePath);
}
catch (Exception ex)
Expand Down Expand Up @@ -107,7 +107,7 @@ protected override async Task<int> Run(string[] unrecognized)
{
options.AddServerHeader = false;
options.AllowSynchronousIO = true;
}).ConfigureKestrel((context, options) =>
}).ConfigureKestrel((_, options) =>
{
var apiListenUri = new Uri(listenUri);

Expand All @@ -125,8 +125,8 @@ protected override async Task<int> Run(string[] unrecognized)
options.Listen(ipAddress, apiListenUri.Port, listenOptions =>
{
#if WINDOWS
listenOptions.UseHttps(StoreName.My, apiListenUri.Host,
location: StoreLocation.LocalMachine, allowInvalid: true);
listenOptions.UseHttps(StoreName.My, apiListenUri.Host,
location: StoreLocation.LocalMachine, allowInvalid: true);
#else
listenOptions.UseHttps();
#endif
Expand All @@ -138,31 +138,52 @@ protected override async Task<int> Run(string[] unrecognized)
}
});

builder
.Host.UseSerilog()
.UseServiceProviderFactory(new AutofacServiceProviderFactory())
.ConfigureContainer<ContainerBuilder>(builder =>
builder.Services.AddSerilog();

builder.Host.UseServiceProviderFactory(new AutofacServiceProviderFactory())
.ConfigureContainer<ContainerBuilder>(containerBuilder =>
{
builder.RegisterBuildCallback(ls => container = ls);
builder.RegisterModule(new ForwarderModule(_storagePath.BufferPath, config));
containerBuilder.RegisterBuildCallback(ls => container = ls);
containerBuilder.RegisterModule(new ForwarderModule(_storagePath.BufferPath, config));
});

using var host = builder.Build();
await using var app = builder.Build();

if (container == null) throw new Exception("Host did not build container.");

app.Use(async (context, next) =>
{
try
{
await next();
}
// ISSUE: this exception type isn't currently used.
catch (RequestProcessingException rex)
{
if (context.Response.HasStarted)
throw;

context.Response.StatusCode = (int)rex.StatusCode;
context.Response.ContentType = "text/plain; charset=UTF-8";
await context.Response.Body.WriteAsync(Encoding.UTF8.GetBytes(rex.Message));
await context.Response.CompleteAsync();
}
});

foreach (var mapper in container.Resolve<IEnumerable<IMapEndpoints>>())
{
mapper.Map(host);
mapper.MapEndpoints(app);
}

var service = container.Resolve<ServerService>(
new TypedParameter(typeof(IHost), host),
new TypedParameter(typeof(IHost), app),
new NamedParameter("listenUri", listenUri));

var exit = ExecutionEnvironment.SupportsStandardIO
? RunStandardIO(service, Console.Out)
? await RunStandardIOAsync(service, Console.Out)
: RunService(service);

Log.Information("Exiting with status code {StatusCode}", exit);

return exit;
}
Expand All @@ -178,6 +199,7 @@ protected override async Task<int> Run(string[] unrecognized)
}

[SuppressMessage("Interoperability", "CA1416:Validate platform compatibility")]
// ReSharper disable once UnusedParameter.Local
static int RunService(ServerService service)
{
#if WINDOWS
Expand All @@ -190,7 +212,7 @@ static int RunService(ServerService service)
#endif
}

static int RunStandardIO(ServerService service, TextWriter cout)
static async Task<int> RunStandardIOAsync(ServerService service, TextWriter cout)
{
service.Start();

Expand All @@ -210,7 +232,7 @@ static int RunStandardIO(ServerService service, TextWriter cout)
Console.Read();
}

service.Stop();
await service.StopAsync();

return 0;
}
Expand All @@ -219,9 +241,9 @@ static void WriteBanner()
{
Write("─", ConsoleColor.DarkGray, 47);
Console.WriteLine();
Write(" Seq Forwarder", ConsoleColor.White);
Write(" SeqCli Forwarder", ConsoleColor.White);
Write(" ──", ConsoleColor.DarkGray);
Write(" © 2024 Datalust Pty Ltd", ConsoleColor.Gray);
Write(" © Datalust Pty Ltd and Contributors", ConsoleColor.Gray);
Console.WriteLine();
Write("─", ConsoleColor.DarkGray, 47);
Console.WriteLine();
Expand All @@ -244,7 +266,7 @@ static Logger CreateLogger(
var loggerConfiguration = new LoggerConfiguration()
.Enrich.FromLogContext()
.Enrich.WithProperty("MachineName", Environment.MachineName)
.Enrich.WithProperty("Application", "Seq Forwarder")
.Enrich.WithProperty("Application", "SeqCli Forwarder")
.MinimumLevel.Is(internalLoggingLevel)
.MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
.WriteTo.File(
Expand Down
2 changes: 1 addition & 1 deletion src/SeqCli/Cli/Commands/Forwarder/StartCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
using SeqCli.Cli;
using SeqCli.Forwarder.ServiceProcess;

namespace Seq.Forwarder.Cli.Commands
namespace SeqCli.Forwarder.Cli.Commands
{
[Command("forwarder", "start", "Start the forwarder Windows service")]
[SuppressMessage("Interoperability", "CA1416:Validate platform compatibility")]
Expand Down
6 changes: 3 additions & 3 deletions src/SeqCli/Cli/Commands/Forwarder/StatusCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
using SeqCli.Cli;
using SeqCli.Forwarder.ServiceProcess;

namespace Seq.Forwarder.Cli.Commands
namespace SeqCli.Forwarder.Cli.Commands
{
[Command("forwarder", "status", "Show the status of the forwarder Windows service")]
[SuppressMessage("Interoperability", "CA1416:Validate platform compatibility")]
Expand All @@ -33,11 +33,11 @@ protected override Task<int> Run()
try
{
var controller = new ServiceController(SeqCliForwarderWindowsService.WindowsServiceName);
Console.WriteLine("The Seq Forwarder service is installed and {0}.", controller.Status.ToString().ToLowerInvariant());
Console.WriteLine($"The {SeqCliForwarderWindowsService.WindowsServiceName} service is installed and {controller.Status.ToString().ToLowerInvariant()}.");
}
catch (InvalidOperationException)
{
Console.WriteLine("The Seq Forwarder service is not installed.");
Console.WriteLine($"The {SeqCliForwarderWindowsService.WindowsServiceName} service is not installed.");
}
catch (Exception ex)
{
Expand Down
2 changes: 1 addition & 1 deletion src/SeqCli/Cli/Commands/Forwarder/StopCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
using SeqCli.Cli;
using SeqCli.Forwarder.ServiceProcess;

namespace Seq.Forwarder.Cli.Commands
namespace SeqCli.Forwarder.Cli.Commands
{
[Command("forwarder", "stop", "Stop the forwarder Windows service")]
[SuppressMessage("Interoperability", "CA1416:Validate platform compatibility")]
Expand Down
4 changes: 2 additions & 2 deletions src/SeqCli/Cli/Commands/Forwarder/UninstallCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@
using System;
using System.IO;
using System.Threading.Tasks;
using Seq.Forwarder.Util;
using SeqCli.Forwarder.Util;
using SeqCli.Cli;
using SeqCli.Forwarder.ServiceProcess;
using SeqCli.Forwarder.Util;

namespace Seq.Forwarder.Cli.Commands
namespace SeqCli.Forwarder.Cli.Commands
{
[Command("forwarder", "uninstall", "Uninstall the forwarder Windows service")]
class UninstallCommand : Command
Expand Down
2 changes: 1 addition & 1 deletion src/SeqCli/Cli/Features/ServiceCredentialsFeature.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

using SeqCli.Cli;

namespace Seq.Forwarder.Cli.Features
namespace SeqCli.Forwarder.Cli.Features
{
class ServiceCredentialsFeature : CommandFeature
{
Expand Down
4 changes: 2 additions & 2 deletions src/SeqCli/Cli/Features/StoragePathFeature.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@ static string GetDefaultStorageRoot()
// Specific to and writable by the current user.
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
#endif
"Seq",
"SeqCli",
"Forwarder"));
}

static string? TryQueryInstalledStorageRoot()
{
#if WINDOWS
if (Seq.Forwarder.Util.ServiceConfiguration.GetServiceStoragePath(
if (SeqCli.Forwarder.Util.ServiceConfiguration.GetServiceStoragePath(
SeqCliForwarderWindowsService.WindowsServiceName, out var storage))
return storage;
#endif
Expand Down
2 changes: 1 addition & 1 deletion src/SeqCli/Forwarder/Diagnostics/InMemorySink.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public IEnumerable<LogEvent> Read()

public void Emit(LogEvent logEvent)
{
if (logEvent == null) throw new ArgumentNullException(nameof(logEvent));
ArgumentNullException.ThrowIfNull(logEvent);
_queue.Enqueue(logEvent);

while (_queue.Count > _queueLength)
Expand Down
2 changes: 1 addition & 1 deletion src/SeqCli/Forwarder/Diagnostics/IngestionLog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public static IEnumerable<LogEvent> Read()
return Sink.Read();
}

public static ILogger ForClient(IPAddress clientHostIP)
public static ILogger ForClient(IPAddress? clientHostIP)
{
return Log.ForContext("ClientHostIP", clientHostIP);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class SeqCliForwarderWindowsService : ServiceBase
{
readonly ServerService _serverService;

public static string WindowsServiceName { get; } = "Seq Forwarder";
public static string WindowsServiceName { get; } = "SeqCli Forwarder";

public SeqCliForwarderWindowsService(ServerService serverService)
{
Expand All @@ -46,7 +46,7 @@ protected override void OnStart(string[] args)

protected override void OnStop()
{
_serverService.Stop();
_serverService.StopAsync().Wait();
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/SeqCli/Forwarder/Storage/LogBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace SeqCli.Forwarder.Storage;

record LogBuffer
class LogBuffer
{
public LogBuffer(Func<CancellationToken, Task> write, CancellationToken cancellationToken)
{
Expand Down Expand Up @@ -42,7 +42,7 @@ public LogBuffer(Func<CancellationToken, Task> write, CancellationToken cancella
public async Task WriteAsync(byte[] storage, Range range, CancellationToken cancellationToken)
{
var tcs = new TaskCompletionSource();
var cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, _shutdownTokenSource.Token);
using var cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, _shutdownTokenSource.Token);

await _writer.WriteAsync(new LogBufferEntry(storage, range, tcs), cts.Token);
await tcs.Task;
Expand Down
7 changes: 7 additions & 0 deletions src/SeqCli/Forwarder/Storage/LogBufferMap.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Threading.Tasks;
using Serilog;

namespace SeqCli.Forwarder.Storage;

Expand All @@ -14,4 +15,10 @@ public LogBuffer Get(string? apiKey)
{
return new LogBuffer(async (c) => await Task.Delay(TimeSpan.FromSeconds(1), c), default);
}

public Task StopAsync()
{
Log.Information("Flushing log buffers");
return Task.CompletedTask;
}
}
2 changes: 1 addition & 1 deletion src/SeqCli/Forwarder/Util/AccountRightsHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

// ReSharper disable FieldCanBeMadeReadOnly.Local

namespace Seq.Forwarder.Util
namespace SeqCli.Forwarder.Util
{
public static class AccountRightsHelper
{
Expand Down
19 changes: 0 additions & 19 deletions src/SeqCli/Forwarder/Util/EnumerableExtensions.cs

This file was deleted.

2 changes: 1 addition & 1 deletion src/SeqCli/Forwarder/Util/ExecutionEnvironment.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#if WINDOWS
using Seq.Forwarder.Util;
using SeqCli.Forwarder.Util;
#endif

namespace SeqCli.Forwarder.Util;
Expand Down
Loading