2020-12-19 18:17:24 +02:00
|
|
|
using Microsoft.AspNetCore.Hosting;
|
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
using Microsoft.Extensions.Hosting;
|
2020-12-20 03:22:27 +02:00
|
|
|
using Serilog;
|
|
|
|
using Serilog.Core;
|
|
|
|
using Serilog.Events;
|
|
|
|
using Serilog.Sinks.MSSqlServer;
|
2020-12-19 18:17:24 +02:00
|
|
|
using System;
|
2020-12-20 03:22:27 +02:00
|
|
|
using System.Diagnostics;
|
|
|
|
using System.IO;
|
2020-12-19 18:17:24 +02:00
|
|
|
using System.Linq;
|
|
|
|
|
|
|
|
namespace IdentityServer.Api
|
|
|
|
{
|
|
|
|
public class Program
|
|
|
|
{
|
|
|
|
public static void Main(string[] args)
|
|
|
|
{
|
2022-02-10 20:21:53 +02:00
|
|
|
var isConsole = args.Contains("--console");
|
2020-12-20 03:22:27 +02:00
|
|
|
if (!isConsole)
|
|
|
|
{
|
|
|
|
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)
|
2021-11-14 11:40:19 +02:00
|
|
|
.AddEnvironmentVariables()
|
2020-12-20 03:22:27 +02:00
|
|
|
.Build();
|
|
|
|
|
|
|
|
var connectionString = configuration.GetConnectionString("DatabaseConnection");
|
|
|
|
var loggingLevelParam = configuration.GetValue<string>("Logging:LogLevel:Default");
|
|
|
|
|
2021-12-05 22:40:13 +02:00
|
|
|
var loggingLevelOk = Enum.TryParse(loggingLevelParam, out LogEventLevel loggingLevel);
|
|
|
|
if (!loggingLevelOk)
|
|
|
|
throw new Exception($"Logging level '{loggingLevelParam}' is not valid.");
|
|
|
|
|
2020-12-20 03:22:27 +02: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);
|
2021-11-13 21:54:08 +02:00
|
|
|
|
|
|
|
var mssqlSinkOptions = new MSSqlServerSinkOptions() { AutoCreateSqlTable = true, TableName = "__Logs" };
|
|
|
|
|
2020-12-20 03:22:27 +02:00
|
|
|
Log.Logger = new LoggerConfiguration()
|
2021-12-05 22:40:13 +02:00
|
|
|
.MinimumLevel.ControlledBy(loggingLevelSwitch)
|
2020-12-20 03:22:27 +02:00
|
|
|
.MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
|
|
|
|
.Enrich.FromLogContext()
|
|
|
|
.WriteTo.Console()
|
2021-11-13 21:54:08 +02:00
|
|
|
.WriteTo.MSSqlServer(connectionString, mssqlSinkOptions, columnOptions: columnOptions)
|
2020-12-20 03:22:27 +02:00
|
|
|
.CreateLogger();
|
|
|
|
|
2021-12-05 22:40:13 +02:00
|
|
|
//Serilog.Debugging.SelfLog.Enable(msg => Console.WriteLine(msg));
|
2021-11-14 11:40:19 +02:00
|
|
|
|
2020-12-20 03:22:27 +02:00
|
|
|
try
|
|
|
|
{
|
|
|
|
var urls = configuration.GetValue<string>("urls");
|
|
|
|
Log.Information("Starting identity server API...");
|
|
|
|
Log.Information($"API listening on {urls}");
|
|
|
|
Console.WriteLine("Application started. Press Ctrl+C to shut down.");
|
|
|
|
CreateHostBuilder(args, configuration, !isConsole).Build().Run();
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
Log.Fatal(ex, "Identity server API host terminated unexpectedly");
|
|
|
|
}
|
|
|
|
finally
|
|
|
|
{
|
|
|
|
Log.CloseAndFlush();
|
|
|
|
}
|
2020-12-19 18:17:24 +02:00
|
|
|
}
|
|
|
|
|
2020-12-20 03:22:27 +02:00
|
|
|
public static IHostBuilder CreateHostBuilder(string[] args, IConfiguration configuration, bool useWindowsService)
|
|
|
|
{
|
|
|
|
var builder = Host.CreateDefaultBuilder(args)
|
|
|
|
.ConfigureWebHostDefaults(webBuilder =>
|
|
|
|
{
|
|
|
|
webBuilder.UseStartup<Startup>()
|
|
|
|
.UseConfiguration(configuration)
|
|
|
|
.UseSerilog();
|
|
|
|
});
|
|
|
|
|
|
|
|
if (useWindowsService)
|
|
|
|
builder.UseWindowsService();
|
|
|
|
|
|
|
|
return builder;
|
|
|
|
}
|
2020-12-19 18:17:24 +02:00
|
|
|
}
|
|
|
|
}
|