-
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathProgram.cs
More file actions
141 lines (116 loc) · 4.72 KB
/
Program.cs
File metadata and controls
141 lines (116 loc) · 4.72 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
using Microsoft.AspNetCore.Server.Kestrel.Core;
using Serilog;
using SystemEnvironment = System.Environment;
namespace SymbolCollector.Server;
public class Program
{
private static readonly string Environment
= SystemEnvironment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Production";
public static IConfiguration Configuration { get; private set; } = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: false)
.AddJsonFile($"appsettings.{Environment}.json", optional: false)
.AddEnvironmentVariables()
.Build();
public static async Task<int> Main(string[] args)
{
if (Environment != "Production")
{
Serilog.Debugging.SelfLog.Enable(Console.Error);
}
Console.WriteLine($"Environment: {Environment}");
Log.Logger = new LoggerConfiguration()
.ReadFrom.Configuration(Configuration)
.CreateLogger();
try
{
Log.Information("Starting with {environment}", Environment);
using var host = CreateHostBuilder(args).Build();
if (args.Length == 1 && args[0] == "--smoke-test")
{
await SmokeTest(host);
}
else
{
await host.RunAsync();
}
return 0;
}
catch (Exception ex)
{
Log.Fatal(ex, "Host terminated unexpectedly");
return 1;
}
finally
{
Log.CloseAndFlush();
}
}
private static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureServices((context, services) =>
{
services.Configure<KestrelServerOptions>(
context.Configuration.GetSection("Kestrel"));
})
.UseSerilog()
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseSentry(o =>
{
o.Dsn = "https://2262a4fa0a6d409c848908ec90c3c5b4@sentry.io/1886021";
o.AddExceptionFilterForType<OperationCanceledException>();
o.MinimumBreadcrumbLevel = LogLevel.Debug;
o.CaptureFailedRequests = true;
o.EnableLogs = true;
o.Experimental.EnableMetrics = true;
// https://github.com/getsentry/symbol-collector/issues/205
// o.CaptureBlockingCalls = true;
o.AddProfilingIntegration(TimeSpan.FromSeconds(2));
o.SetBeforeSend(@event =>
{
// Stop raising warning that endpoint was overriden
if (@event.Message?.Formatted?.Contains(@"Binding to endpoints defined in") == true
&& @event.Level == SentryLevel.Warning)
{
return null!;
}
// Don't capture Debug events
if (@event.Level == SentryLevel.Debug)
{
return null;
}
return @event;
});
#pragma warning disable SENTRY0001
o.EnableHeapDumps(20, Debouncer.PerDay(3, TimeSpan.FromHours(3)));
#pragma warning restore SENTRY0001
});
webBuilder.UseStartup<Startup>();
});
private static async Task SmokeTest(IHost host)
{
var cancellationTokenSource = new CancellationTokenSource();
var configuration = host.Services.GetRequiredService<IConfiguration>();
var url = configuration.GetValue<string>("Kestrel:EndPoints:Http:Url");
if (string.IsNullOrWhiteSpace(url))
{
throw new InvalidOperationException("Kestrel url is required");
}
// host.StartAsync and client.GetAsync combined will need to take less than:
cancellationTokenSource.CancelAfter(TimeSpan.FromSeconds(3));
await host.StartAsync(cancellationTokenSource.Token);
using var client = new HttpClient();
using var response = await client.GetAsync(new Uri(new Uri(url, UriKind.Absolute), "/smoke-test"),
cancellationTokenSource.Token);
if (response.IsSuccessStatusCode)
{
Log.Information("Health check passed.");
cancellationTokenSource.Cancel(); // Stops the host, graceful shutdown.
}
else
{
throw new Exception($"Health check failed with status code: {response.StatusCode}.");
}
}
}