68 lines
2.0 KiB
C#
68 lines
2.0 KiB
C#
using Birdmap.DAL;
|
|
using Microsoft.AspNetCore.Hosting;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Hosting;
|
|
using Microsoft.Extensions.Logging;
|
|
using NLog;
|
|
using NLog.Web;
|
|
using System;
|
|
|
|
namespace Birdmap.API
|
|
{
|
|
public class Program
|
|
{
|
|
public static void Main(string[] args)
|
|
{
|
|
var logger = NLogBuilder.ConfigureNLog("NLog.config").GetCurrentClassLogger();
|
|
|
|
try
|
|
{
|
|
logger.Debug("Building host...");
|
|
var host = CreateHostBuilder(args).Build();
|
|
|
|
using (var scope = host.Services.CreateScope())
|
|
{
|
|
var db = scope.ServiceProvider.GetRequiredService<BirdmapContext>();
|
|
db.Database.EnsureCreated();
|
|
}
|
|
|
|
logger.Debug("Seeding database...");
|
|
SeedDatabase(host);
|
|
|
|
logger.Debug("Running host...");
|
|
host.Run();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
logger.Error(ex, "Exception occurred in Main.");
|
|
throw;
|
|
}
|
|
finally
|
|
{
|
|
LogManager.Shutdown();
|
|
}
|
|
}
|
|
|
|
public static IHostBuilder CreateHostBuilder(string[] args) =>
|
|
Host.CreateDefaultBuilder(args)
|
|
.ConfigureWebHostDefaults(webBuilder =>
|
|
{
|
|
webBuilder.UseStartup<Startup>();
|
|
}).ConfigureLogging(logging =>
|
|
{
|
|
logging.ClearProviders();
|
|
logging.AddConsole();
|
|
logging.SetMinimumLevel(Microsoft.Extensions.Logging.LogLevel.Trace);
|
|
})
|
|
.UseNLog();
|
|
|
|
private static void SeedDatabase(IHost host)
|
|
{
|
|
using var scope = host.Services.CreateScope();
|
|
var dbInitializer = scope.ServiceProvider.GetRequiredService<DbInitializer>();
|
|
|
|
dbInitializer.Initialize();
|
|
}
|
|
}
|
|
}
|