-
Notifications
You must be signed in to change notification settings - Fork 654
Expand file tree
/
Copy pathProgram.cs
More file actions
73 lines (65 loc) · 2.38 KB
/
Copy pathProgram.cs
File metadata and controls
73 lines (65 loc) · 2.38 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
using System.IO;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Serilog;
using Serilog.Events;
using Titanium.Web.Proxy.Examples.WindowsService;
// File-first logging: rolling files get Information+; Event Log is Warning+ so request volume
// does not flood Event Viewer. Console is useful when run interactively (`dotnet run`).
var logDirectory = Path.Combine(AppContext.BaseDirectory, "logs");
Directory.CreateDirectory(logDirectory);
#if DEBUG
const LogEventLevel minimumLevel = LogEventLevel.Verbose;
#else
const LogEventLevel minimumLevel = LogEventLevel.Information;
#endif
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Is(minimumLevel)
.MinimumLevel.Override("Microsoft.Hosting.Lifetime", LogEventLevel.Information)
.Enrich.FromLogContext()
.WriteTo.Console()
.WriteTo.File(
path: Path.Combine(logDirectory, "proxy-service.log"),
rollingInterval: RollingInterval.Day,
fileSizeLimitBytes: 10 * 1024 * 1024,
retainedFileCountLimit: 5,
shared: true)
.WriteTo.EventLog(
source: "Titanium Web Proxy",
logName: "Application",
manageEventSource: false,
restrictedToMinimumLevel: LogEventLevel.Warning)
.CreateLogger();
try
{
try
{
Console.Title = "Titanium Web Proxy";
}
catch (IOException)
{
// No console when running under the Service Control Manager.
}
var builder = Host.CreateApplicationBuilder(args);
// Registers this process as a Windows Service host when launched by the Service Control Manager
// (falls back to a normal console app when run interactively, e.g. `dotnet run`).
// Must match the service Name in install.ps1 / remove.ps1.
builder.Services.AddWindowsService(options => options.ServiceName = "TitaniumWebProxy");
// Serilog owns all sinks (file, Event Log Warning+, console); default Microsoft logging providers are cleared.
builder.Logging.ClearProviders();
builder.Services.AddSerilog();
builder.Services.Configure<ProxySettings>(builder.Configuration.GetSection("ProxySettings"));
builder.Services.AddHostedService<ProxyWorker>();
var host = builder.Build();
await host.RunAsync();
}
catch (Exception ex)
{
Log.Fatal(ex, "Proxy service terminated unexpectedly");
throw;
}
finally
{
await Log.CloseAndFlushAsync();
}