chatbot/Chatbot.Api/Program.cs

89 lines
3.2 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)
.AddEnvironmentVariables()
2020-06-06 18:29:20 +03:00
.Build();
var connectionString = configuration.GetConnectionString("DatabaseConnection");
var loggingLevelParam = configuration.GetValue<string>("Logging:LogLevel:Default");
2022-09-03 11:12:05 +03:00
var loggingLevelOk = Enum.TryParse(loggingLevelParam, out LogEventLevel loggingLevel);
if (!loggingLevelOk)
throw new Exception($"Logging level '{loggingLevelParam}' is not valid.");
2020-06-06 18:29:20 +03:00
var loggingLevelSwitch = new LoggingLevelSwitch(loggingLevel);
var columnOptions = new ColumnOptions();
columnOptions.Store.Remove(StandardColumn.Properties);
columnOptions.Store.Remove(StandardColumn.MessageTemplate);
columnOptions.Store.Add(StandardColumn.LogEvent);
2022-09-03 11:12:05 +03:00
var mssqlSinkOptions = new MSSqlServerSinkOptions() { AutoCreateSqlTable = true, TableName = "__Logs" };
2020-06-06 18:29:20 +03:00
Log.Logger = new LoggerConfiguration()
.MinimumLevel.ControlledBy(loggingLevelSwitch)
.MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
.Enrich.FromLogContext()
.WriteTo.Console()
2022-09-03 11:12:05 +03:00
.WriteTo.MSSqlServer(connectionString, mssqlSinkOptions, columnOptions: columnOptions)
2020-06-06 18:29:20 +03:00
.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
}
2022-09-03 11:12:05 +03:00
public static IHostBuilder CreateHostBuilder(string[] args, IConfiguration configuration)
{
var builder = Host.CreateDefaultBuilder(args)
2020-06-06 16:07:32 +03:00
.ConfigureWebHostDefaults(webBuilder =>
{
2020-06-06 18:29:20 +03:00
webBuilder.UseStartup<Startup>()
2022-09-03 11:12:05 +03:00
.UseConfiguration(configuration);
2020-06-06 16:07:32 +03:00
});
2022-09-03 11:12:05 +03:00
builder.UseSerilog();
return builder;
}
2020-06-06 16:07:32 +03:00
}
}