You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
82 lines
2.5 KiB
82 lines
2.5 KiB
using BookStore.Data; |
|
using Microsoft.AspNetCore.Builder; |
|
using Microsoft.Extensions.DependencyInjection; |
|
using Microsoft.Extensions.Hosting; |
|
using Serilog; |
|
using Serilog.Events; |
|
using System; |
|
using System.Linq; |
|
using System.Threading.Tasks; |
|
|
|
namespace BookStore; |
|
|
|
public class Program |
|
{ |
|
public async static Task<int> Main(string[] args) |
|
{ |
|
AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true); |
|
var loggerConfiguration = new LoggerConfiguration() |
|
#if DEBUG |
|
.MinimumLevel.Debug() |
|
#else |
|
.MinimumLevel.Information() |
|
#endif |
|
.MinimumLevel.Override("Microsoft", LogEventLevel.Information) |
|
.MinimumLevel.Override("Microsoft.EntityFrameworkCore", LogEventLevel.Warning) |
|
.Enrich.FromLogContext() |
|
#if DEBUG |
|
.WriteTo.Async(c => c.File("Logs/logs.txt")) |
|
.WriteTo.Async(c => c.Console()); |
|
#else |
|
.WriteTo.Async(c => c.File("Logs/logs.txt")); |
|
#endif |
|
if (IsMigrateDatabase(args)) |
|
{ |
|
loggerConfiguration.MinimumLevel.Override("Volo.Abp", LogEventLevel.Warning); |
|
loggerConfiguration.MinimumLevel.Override("Microsoft", LogEventLevel.Warning); |
|
loggerConfiguration.MinimumLevel.Override("IdentityServer4.Startup", LogEventLevel.Warning); |
|
} |
|
|
|
Log.Logger = loggerConfiguration.CreateLogger(); |
|
|
|
try |
|
{ |
|
var builder = WebApplication.CreateBuilder(args); |
|
builder.Host.AddAppSettingsSecretsJson() |
|
.UseAutofac() |
|
.UseSerilog(); |
|
await builder.AddApplicationAsync<BookStoreModule>(); |
|
var app = builder.Build(); |
|
await app.InitializeApplicationAsync(); |
|
|
|
if (IsMigrateDatabase(args)) |
|
{ |
|
await app.Services.GetRequiredService<BookStoreDbMigrationService>().MigrateAsync(); |
|
return 0; |
|
} |
|
|
|
Log.Information("Starting BookStore."); |
|
await app.RunAsync(); |
|
return 0; |
|
} |
|
catch (Exception ex) |
|
{ |
|
if (ex.GetType().Name.Equals("StopTheHostException", StringComparison.Ordinal)) |
|
{ |
|
throw; |
|
} |
|
|
|
Log.Fatal(ex, "BookStore terminated unexpectedly!"); |
|
return 1; |
|
} |
|
finally |
|
{ |
|
Log.CloseAndFlush(); |
|
} |
|
} |
|
|
|
private static bool IsMigrateDatabase(string[] args) |
|
{ |
|
return args.Any(x => x.Contains("--migrate-database", StringComparison.OrdinalIgnoreCase)); |
|
} |
|
}
|
|
|