-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
149 lines (129 loc) · 4.57 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
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
using Serilog;
using Serilog.Exceptions;
using Serilog.Sinks.Elasticsearch;
using System.Reflection;
using Quartz;
using Quartz.AspNetCore;
using GrafanaLoki.Jobs;
using Elastic.Apm.NetCoreAll;
using System.Configuration;
var builder = WebApplication.CreateBuilder(args);
ConfigureLogging();
// Add services to the container.
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Host.UseSerilog();
Serilog.Debugging.SelfLog.Enable(Console.Error);
//var credentials = new BasicAuthCredentials("http://localhost:3100", "admin", "Admin@123"); // Address to local or remote Loki server
//Log.Logger = new LoggerConfiguration()
// .MinimumLevel.Override("Microsoft", Serilog.Events.LogEventLevel.Warning)
// .MinimumLevel.Override("Microsoft", Serilog.Events.LogEventLevel.Error)
// .Enrich.FromLogContext()
// .WriteTo.LokiHttp(credentials)
// .CreateLogger();
builder.Services.AddQuartz(q =>
{
var timeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById("Bangladesh Standard Time");
var jobKey = new JobKey("job1");
q.AddJob<SendEmailJob>(opts => opts.WithIdentity(jobKey));
q.AddTrigger(opts => opts
.ForJob(jobKey)
.WithIdentity("trigger1")
.WithCronSchedule("0 * * * * ?", x => // Run Every single minute.
//.WithCronSchedule("0 0 0 * * ?", x => // Run Every day at midnight (00:00:00).
{
x.InTimeZone(timeZoneInfo);
})
.StartNow()
);
});
builder.Services.AddQuartzHostedService(q => q.WaitForJobsToComplete = true);
// ASP.NET Core hosting
//builder.Services.AddQuartzServer(options =>
//{
// // when shutting down we want jobs to complete gracefully
// options.WaitForJobsToComplete = true;
//});
var app = builder.Build();
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseAllElasticApm(builder.Configuration);
app.UseSerilogRequestLogging();
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();
void ConfigureLogging()
{
//var credentials = new BasicAuthCredentials("http://localhost:3100", "admin", "Admin@123"); // Address to local or remote Loki server
var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Production";
var configuration = new ConfigurationBuilder()
.SetBasePath(AppDomain.CurrentDomain.BaseDirectory)
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile($"appsettings.{environment}.json", optional: true)
.Build();
Log.Logger = new LoggerConfiguration()
.Enrich.FromLogContext()
.Enrich.WithExceptionDetails()
.Enrich.WithMachineName()
.WriteTo.Debug()
.WriteTo.Console()
.WriteTo.Elasticsearch(ConfigureElasticSink(configuration, environment))
//.WriteTo.LokiHttp(credentials)
.Enrich.WithProperty("Environment", environment)
.ReadFrom.Configuration(configuration)
.CreateLogger();
}
static ElasticsearchSinkOptions ConfigureElasticSink(IConfigurationRoot configuration, string environment)
{
//elasticsearch video tutorial.
//https://www.youtube.com/watch?v=zp6A5QCW_II
// https://www.humankode.com/asp-net-core/logging-with-elasticsearch-kibana-asp-net-core-and-docker/
var x = new ElasticsearchSinkOptions(new Uri(configuration["ElasticConfiguration:Uri"]))
{
AutoRegisterTemplate = true,
IndexFormat = $"{Assembly.GetExecutingAssembly().GetName().Name.ToLower().Replace(".", "-")}-{environment?.ToLower().Replace(".", "-")}-{DateTime.UtcNow:yyyy-MM}",
NumberOfReplicas = 1 ,
NumberOfShards = 2 ,
};
return x;
}
#region Elasticsearch kibana docker-compose.yml file.
//version: '3.1'
//services:
//elasticsearch:
//container_name: elasticsearch
//image: docker.elastic.co / elasticsearch / elasticsearch:7.9.2
// ports:
//-9200:9200
// volumes:
//-elasticsearch - data:/ usr / share / elasticsearch / data
// environment:
//-xpack.monitoring.enabled = true
//- xpack.watcher.enabled = false
//- "ES_JAVA_OPTS=-Xms512m -Xmx512m"
//- discovery.type = single - node
// networks:
//-elastic
// kibana:
//container_name: kibana
//image: docker.elastic.co / kibana / kibana:7.9.2
// ports:
//-5601:5601
// depends_on:
//-elasticsearch
// environment:
//-ELASTICSEARCH_URL = http://localhost:9200
// networks:
//-elastic
//networks:
//elastic:
//driver: bridge
//volumes:
// elasticsearch - data:
#endregion