forked from datalust/seqcli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRunCommand.cs
More file actions
308 lines (264 loc) · 10.3 KB
/
RunCommand.cs
File metadata and controls
308 lines (264 loc) · 10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
// Copyright © Datalust Pty Ltd
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
using Autofac;
using Autofac.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;
using SeqCli.Api;
using SeqCli.Cli.Features;
using SeqCli.Config;
using SeqCli.Forwarder;
using SeqCli.Forwarder.Util;
using SeqCli.Forwarder.Web.Api;
using SeqCli.Forwarder.Web.Host;
using Serilog;
using Serilog.Core;
using Serilog.Events;
using Serilog.Formatting.Compact;
#if WINDOWS
using System.Security.Cryptography.X509Certificates;
using SeqCli.Forwarder.ServiceProcess;
#endif
// ReSharper disable UnusedType.Global
namespace SeqCli.Cli.Commands.Forwarder;
[Command("forwarder", "run", "Listen on an HTTP endpoint and forward ingested logs to Seq", IsPreview = true)]
class RunCommand : Command
{
readonly StoragePathFeature _storagePath;
readonly ListenUriFeature _listenUri;
readonly ConnectionFeature _connection;
bool _noLogo;
public RunCommand()
{
Options.Add("nologo", _ => _noLogo = true);
_listenUri = Enable<ListenUriFeature>();
_connection = Enable<ConnectionFeature>();
_storagePath = Enable<StoragePathFeature>();
}
protected override async Task<int> Run(string[] unrecognized)
{
if (Environment.UserInteractive)
{
if (!_noLogo)
{
WriteBanner();
Console.WriteLine();
}
Console.WriteLine("Running as server; press Ctrl+C to exit.");
Console.WriteLine();
}
SeqCliConfig config;
try
{
config = RuntimeConfigurationLoader.Load(_storagePath);
}
catch (Exception ex)
{
await using var logger = CreateLogger(
LogEventLevel.Information,
_storagePath.InternalLogPath);
logger.Fatal(ex, "Failed to load configuration from {ConfigFilePath}", _storagePath.ConfigFilePath);
return 1;
}
var connection = SeqConnectionFactory.Connect(_connection, config);
// The API key is passed through separately because `SeqConnection` doesn't expose a batched ingestion
// mechanism and so we manually construct `HttpRequestMessage`s deeper in the stack. Nice feature gap to
// close at some point!
var (serverUrl, apiKey) = SeqConnectionFactory.GetConnectionDetails(_connection, config);
Log.Logger = CreateLogger(
config.Forwarder.Diagnostics.InternalLoggingLevel,
_storagePath.InternalLogPath,
config.Forwarder.Diagnostics.InternalLogServerUri,
config.Forwarder.Diagnostics.InternalLogServerApiKey);
Log.Information("Loaded configuration from {ConfigFilePath}", _storagePath.ConfigFilePath);
Log.Information("Forwarding to {ServerUrl}", serverUrl);
var listenUri = _listenUri.ListenUri ?? config.Forwarder.Api.ListenUri;
try
{
ILifetimeScope? container = null;
var builder = WebApplication.CreateBuilder();
builder.WebHost.UseKestrel(options =>
{
options.AddServerHeader = false;
options.AllowSynchronousIO = true;
}).ConfigureKestrel((_, options) =>
{
var apiListenUri = new Uri(listenUri);
var ipAddress = apiListenUri.HostNameType switch
{
UriHostNameType.Basic => IPAddress.Any,
UriHostNameType.Dns => IPAddress.Any,
UriHostNameType.IPv4 => IPAddress.Parse(apiListenUri.Host),
UriHostNameType.IPv6 => IPAddress.Parse(apiListenUri.Host),
_ => throw new NotSupportedException($"Listen URI type `{apiListenUri.HostNameType}` is not supported.")
};
if (apiListenUri.Scheme.Equals("https", StringComparison.OrdinalIgnoreCase))
{
options.Listen(ipAddress, apiListenUri.Port, listenOptions =>
{
#if WINDOWS
listenOptions.UseHttps(StoreName.My, apiListenUri.Host,
location: StoreLocation.LocalMachine, allowInvalid: true);
#else
listenOptions.UseHttps();
#endif
});
}
else
{
options.Listen(ipAddress, apiListenUri.Port);
}
});
builder.Services.AddSerilog();
builder.Host.UseServiceProviderFactory(new AutofacServiceProviderFactory())
.ConfigureContainer<ContainerBuilder>(containerBuilder =>
{
containerBuilder.RegisterBuildCallback(ls => container = ls);
containerBuilder.RegisterModule(new ForwarderModule(_storagePath.BufferPath, config, connection, apiKey));
});
await using var app = builder.Build();
if (container == null) throw new Exception("Host did not build container.");
foreach (var mapper in container.Resolve<IEnumerable<IMapEndpoints>>())
{
mapper.MapEndpoints(app);
}
var service = container.Resolve<ServerService>(
new TypedParameter(typeof(IHost), app),
new NamedParameter("listenUri", listenUri));
var exit = ExecutionEnvironment.SupportsStandardIO
? await RunStandardIOAsync(service, Console.Out)
: RunService(service);
Log.Information("Exiting with status code {StatusCode}", exit);
return exit;
}
catch (Exception ex)
{
Log.Fatal(ex, "Unhandled exception");
return -1;
}
finally
{
await Log.CloseAndFlushAsync();
}
}
[SuppressMessage("Interoperability", "CA1416:Validate platform compatibility")]
// ReSharper disable once UnusedParameter.Local
static int RunService(ServerService service)
{
#if WINDOWS
System.ServiceProcess.ServiceBase.Run([
new SeqCliForwarderWindowsService(service)
]);
return 0;
#else
throw new NotSupportedException("Windows services are not supported on this platform.");
#endif
}
static async Task<int> RunStandardIOAsync(ServerService service, TextWriter cout)
{
service.Start();
var waitForShutDownRequest = new TaskCompletionSource();
var done = new ManualResetEventSlim(false);
void ShutDown()
{
waitForShutDownRequest.TrySetResult();
done.Wait();
}
try
{
Console.TreatControlCAsInput = false;
Console.CancelKeyPress += (_, _) =>
{
cout.WriteLine("Ctrl+C pressed; stopping...");
Log.Information("Interrupt signal received");
ShutDown();
};
}
catch (IOException)
{
Log.Information("Disabling Ctrl+C handling; process is non-interactive");
}
AppDomain.CurrentDomain.ProcessExit += (_, _) =>
{
Log.Information("Termination signal received");
ShutDown();
};
try
{
await waitForShutDownRequest.Task;
await service.StopAsync();
}
finally
{
done.Set();
}
return 0;
}
static void WriteBanner()
{
Write("─", ConsoleColor.DarkGray, 47);
Console.WriteLine();
Write(" SeqCli Forwarder", ConsoleColor.White);
Write(" ──", ConsoleColor.DarkGray);
Write(" © Datalust Pty Ltd and Contributors", ConsoleColor.Gray);
Console.WriteLine();
Write("─", ConsoleColor.DarkGray, 47);
Console.WriteLine();
}
static void Write(string s, ConsoleColor color, int repeats = 1)
{
Console.ForegroundColor = color;
for (var i = 0; i < repeats; ++i)
Console.Write(s);
Console.ResetColor();
}
static Logger CreateLogger(
LogEventLevel internalLoggingLevel,
string internalLogPath,
string? internalLogServerUri = null,
string? internalLogServerApiKey = null)
{
var loggerConfiguration = new LoggerConfiguration()
.Enrich.FromLogContext()
.Enrich.WithProperty("MachineName", Environment.MachineName)
.Enrich.WithProperty("Application", "SeqCli Forwarder")
.MinimumLevel.Is(internalLoggingLevel)
.MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
.WriteTo.File(
new RenderedCompactJsonFormatter(),
GetRollingLogFilePathFormat(internalLogPath),
rollingInterval: RollingInterval.Day,
fileSizeLimitBytes: 1024 * 1024);
if (Environment.UserInteractive)
loggerConfiguration.WriteTo.Console(restrictedToMinimumLevel: LogEventLevel.Information);
if (!string.IsNullOrWhiteSpace(internalLogServerUri))
loggerConfiguration.WriteTo.Seq(
internalLogServerUri,
apiKey: internalLogServerApiKey);
return loggerConfiguration.CreateLogger();
}
static string GetRollingLogFilePathFormat(string internalLogPath)
{
if (internalLogPath == null) throw new ArgumentNullException(nameof(internalLogPath));
return Path.Combine(internalLogPath, "seq-forwarder-.log");
}
}