-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
64 lines (54 loc) · 1.8 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
using Microsoft.Data.SqlClient;
using Microsoft.EntityFrameworkCore;
using Nest;
using Serilog;
using Serilog.Events;
using SQLServerVsElastic.Data;
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Override("System", LogEventLevel.Warning)
.MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
.MinimumLevel.Override("Microsoft.EntityFrameworkCore", LogEventLevel.Warning)
.MinimumLevel.Override("Microsoft.AspNetCore", LogEventLevel.Warning)
.MinimumLevel.Override("Microsoft.Hosting.Lifetime", LogEventLevel.Information)
.WriteTo.Console()
.CreateLogger();
try
{
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllers();
builder.Services.AddSerilog();
var connectionStringBuilder = new SqlConnectionStringBuilder(builder.Configuration.GetConnectionString("SqlServer"))
{
Encrypt = true,
TrustServerCertificate = true,
IntegratedSecurity = false
};
builder.Services.AddDbContext<TestDbContext>(options =>
options.UseSqlServer(connectionStringBuilder.ConnectionString));
builder.Services.AddSingleton<IElasticClient>(new ElasticClient(
new ConnectionSettings(new Uri("http://localhost:9200"))
));
var app = builder.Build();
using (var scope = app.Services.CreateScope())
{
var dbContext = scope.ServiceProvider.GetRequiredService<TestDbContext>();
dbContext.Database.Migrate();
}
app.UseSerilogRequestLogging(options =>
{
options.IncludeQueryInRequestPath = true;
});
app.UseRouting();
app.MapControllerRoute(
name: "default",
pattern: "api/{controller}/{action=Index}/{id?}");
app.Run();
}
catch (Exception ex)
{
Log.Fatal(ex, "Application terminated unexpectedly");
}
finally
{
Log.CloseAndFlush();
}