Squirrowse/Squirrowse.Service/Program.cs
danielgrabowski e91a5143dd Codecleanup
2019-11-04 09:29:37 +01:00

66 lines
2.1 KiB
C#

using System;
using System.IO;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using NLog;
using NLog.Extensions.Logging;
using NLog.Web;
namespace Squirrowse.Service
{
public class Program
{
public static IConfigurationRoot Configuration { get; set; }
public static void Main(string[] args)
{
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();
}
}
public static IHostBuilder CreateHostBuilder(string[] args)
{
return Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
webBuilder.UseConfiguration(Configuration);
webBuilder.UseNLog();
});
}
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;
}
}
}