-
Notifications
You must be signed in to change notification settings - Fork 1
/
Program.cs
85 lines (77 loc) · 2.99 KB
/
Program.cs
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
using System;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;
using Serilog;
using Serilog.Events;
using Serilog.Formatting.Compact;
using Amazon;
using Serilog.Configuration;
using Serilog.Core;
using System.Text;
using Serilog.Sinks.AmazonS3;
using RollingInterval = Serilog.Sinks.AmazonS3.RollingInterval;
using Serilog.Sinks.PeriodicBatching;
namespace VSMarketplaceBadges
{
public class Program
{
public static int Main(string[] args)
{
var logConf = new LoggerConfiguration()
.Enrich.FromLogContext();
string env = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
if (env == Microsoft.Extensions.Hosting.Environments.Development)
logConf.WriteTo.Console(new RenderedCompactJsonFormatter());
else if (env == Microsoft.Extensions.Hosting.Environments.Production)
AmazonS3(logConf.WriteTo, "logs/app.log", "vsmarketplace-badges/logs", RegionEndpoint.APNortheast1);
Log.Logger = logConf.CreateLogger();
Log.Information($"env:{env}");
try
{
CreateHostBuilder(args).Build().Run();
return 0;
}
catch (Exception ex)
{
Log.Fatal(ex, "Host terminated unexpectedly");
return 1;
}
finally
{
Log.CloseAndFlush();
}
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.UseSerilog()
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
public static LoggerConfiguration AmazonS3(LoggerSinkConfiguration sinkConfiguration, string path, string bucketName, RegionEndpoint endpoint)
{
var options = new AmazonS3Options
{
Path = path,
BucketName = bucketName,
Endpoint = endpoint,
OutputTemplate = "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {Message:lj}{NewLine}{Exception}",
FormatProvider = null,
RollingInterval = RollingInterval.Hour,
Encoding = Encoding.UTF8,
FailureCallback = null,
BucketPath = null
};
var amazonS3Sink = new AmazonS3Sink(options);
var batchingOptions = new PeriodicBatchingSinkOptions
{
BatchSizeLimit = 5000,
Period = TimeSpan.FromSeconds(5),
EagerlyEmitFirstEvent = true,
QueueLimit = 10000
};
var batchingSink = new PeriodicBatchingSink(amazonS3Sink, batchingOptions);
return sinkConfiguration.Sink(batchingSink, LevelAlias.Minimum, null);
}
}
}