chatbot/Chatbot.Api/Program.cs

77 lines
2.9 KiB
C#
Raw Normal View History

2020-06-06 16:07:32 +03:00
using System;
2020-06-06 18:29:20 +03:00
using System.Diagnostics;
using System.IO;
2020-06-06 16:07:32 +03:00
using System.Linq;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
2020-06-06 18:29:20 +03:00
using Serilog;
using Serilog.Core;
using Serilog.Events;
using Serilog.Sinks.MSSqlServer;
2020-06-06 16:07:32 +03:00
namespace Chatbot.Api
{
public class Program
{
public static void Main(string[] args)
{
2020-06-06 18:29:20 +03:00
if (!args.Contains("--docker"))
{
var pathToExe = Process.GetCurrentProcess().MainModule.FileName;
var pathToContentRoot = Path.GetDirectoryName(pathToExe);
Directory.SetCurrentDirectory(pathToContentRoot);
}
var configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.Build();
var connectionString = configuration.GetConnectionString("DatabaseConnection");
var loggingLevelParam = configuration.GetValue<string>("Logging:LogLevel:Default");
Enum.TryParse(loggingLevelParam, out LogEventLevel loggingLevel);
var loggingLevelSwitch = new LoggingLevelSwitch(loggingLevel);
var columnOptions = new ColumnOptions();
columnOptions.Store.Remove(StandardColumn.Properties);
columnOptions.Store.Remove(StandardColumn.MessageTemplate);
columnOptions.Store.Add(StandardColumn.LogEvent);
Log.Logger = new LoggerConfiguration()
.MinimumLevel.ControlledBy(loggingLevelSwitch)
.MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
.Enrich.FromLogContext()
.WriteTo.Console()
.WriteTo.MSSqlServer(connectionString, "__Logs_Api", autoCreateSqlTable: true, columnOptions: columnOptions)
.CreateLogger();
try
{
var urls = configuration.GetValue<string>("urls");
Log.Information("Starting chatbot API...");
Log.Information($"API listening on {urls}");
Console.WriteLine("Application started. Press Ctrl+C to shut down.");
CreateHostBuilder(args, configuration).Build().Run();
}
catch (Exception ex)
{
Log.Fatal(ex, "Chatbot API host terminated unexpectedly");
}
finally
{
Log.CloseAndFlush();
}
2020-06-06 16:07:32 +03:00
}
2020-06-06 18:29:20 +03:00
public static IHostBuilder CreateHostBuilder(string[] args, IConfiguration configuration) =>
2020-06-06 16:07:32 +03:00
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
2020-06-06 18:29:20 +03:00
webBuilder.UseStartup<Startup>()
.UseConfiguration(configuration)
.UseSerilog();
2020-06-06 16:07:32 +03:00
});
}
}