Squirrowse/Squirrowse.Service/Program.cs

69 lines
2.2 KiB
C#
Raw Normal View History

2019-10-31 10:23:07 +01:00
using System;
using System.Collections.Generic;
2019-10-31 12:53:57 +01:00
using System.IO;
2019-10-31 10:23:07 +01:00
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
2019-10-31 12:53:57 +01:00
using NLog;
using NLog.Extensions.Logging;
using NLog.Web;
2019-10-31 10:23:07 +01:00
namespace Squirrowse.Service
{
public class Program
{
2019-10-31 12:53:57 +01:00
public static IConfigurationRoot Configuration { get; set; }
2019-10-31 10:23:07 +01:00
public static void Main(string[] args)
{
2019-10-31 12:53:57 +01:00
Configuration = CreateConfigurationBuilder().Build();
var logger = GetLogger();
try
{
logger.Debug("Application started");
CreateHostBuilder(args)
.Build()
.Run();
}
catch (Exception ex)
{
logger.Error(ex, "Stopped program because of exception when building WebHost");
throw;
}
finally
{
LogManager.Shutdown();
}
2019-10-31 10:23:07 +01:00
}
2019-10-31 12:53:57 +01:00
public static IHostBuilder CreateHostBuilder(string[] args)
{
return Host.CreateDefaultBuilder(args)
2019-10-31 10:23:07 +01:00
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
2019-10-31 12:53:57 +01:00
webBuilder.UseConfiguration(Configuration);
webBuilder.UseNLog();
2019-10-31 10:23:07 +01:00
});
2019-10-31 12:53:57 +01:00
}
private static IConfigurationBuilder CreateConfigurationBuilder()
{
var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
return new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", false, true)
.AddJsonFile($"appsettings.{environment}.json", false, true);
}
private static ILogger GetLogger()
{
var nlogConfigSection = Configuration.GetSection("NLog");
LogManager.Configuration = new NLogLoggingConfiguration(nlogConfigSection);
ILogger logger = LogManager.GetCurrentClassLogger();
return logger;
}
2019-10-31 10:23:07 +01:00
}
}