diff --git a/NetworkResurrector.Api/NetworkResurrector.Api.csproj b/NetworkResurrector.Api/NetworkResurrector.Api.csproj index 602886b..6f5a4b1 100644 --- a/NetworkResurrector.Api/NetworkResurrector.Api.csproj +++ b/NetworkResurrector.Api/NetworkResurrector.Api.csproj @@ -18,6 +18,7 @@ + diff --git a/NetworkResurrector.Api/Program.cs b/NetworkResurrector.Api/Program.cs index 60957b0..728d626 100644 --- a/NetworkResurrector.Api/Program.cs +++ b/NetworkResurrector.Api/Program.cs @@ -1,11 +1,14 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Hosting; -using Microsoft.Extensions.Logging; +using Serilog; +using Serilog.Core; +using Serilog.Events; +using Serilog.Sinks.MSSqlServer; +using System; +using System.Diagnostics; +using System.IO; +using System.Linq; namespace NetworkResurrector.Api { @@ -13,14 +16,70 @@ namespace NetworkResurrector.Api { public static void Main(string[] args) { - CreateHostBuilder(args).Build().Run(); + var isConsole = Debugger.IsAttached || args.Contains("--console"); + 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) + .Build(); + + var connectionString = configuration.GetConnectionString("DatabaseConnection"); + var loggingLevelParam = configuration.GetValue("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", autoCreateSqlTable: true, columnOptions: columnOptions) + .CreateLogger(); + + try + { + var urls = configuration.GetValue("urls"); + Log.Information("Starting network resurrector 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, "Network resurrector API host terminated unexpectedly"); + } + finally + { + Log.CloseAndFlush(); + } + } - public static IHostBuilder CreateHostBuilder(string[] args) => - Host.CreateDefaultBuilder(args) - .ConfigureWebHostDefaults(webBuilder => - { - webBuilder.UseStartup(); - }); + public static IHostBuilder CreateHostBuilder(string[] args, IConfiguration configuration, bool useWindowsService) + { + var builder = Host.CreateDefaultBuilder(args) + .ConfigureWebHostDefaults(webBuilder => + { + webBuilder.UseStartup() + .UseConfiguration(configuration) + .UseSerilog(); + }); + + if (useWindowsService) + builder.UseWindowsService(); + + return builder; + } } } diff --git a/NetworkResurrector.Api/appsettings.json b/NetworkResurrector.Api/appsettings.json index d9d9a9b..5119161 100644 --- a/NetworkResurrector.Api/appsettings.json +++ b/NetworkResurrector.Api/appsettings.json @@ -1,7 +1,11 @@ { + "urls": "http://*:5062", + "ConnectionStrings": { + "DatabaseConnection": "***REMOVED***" + }, "Logging": { "LogLevel": { - "Default": "Information", + "Default": "Debug", "Microsoft": "Warning", "Microsoft.Hosting.Lifetime": "Information" } diff --git a/dependencies.props b/dependencies.props index b606756..0bd60d6 100644 --- a/dependencies.props +++ b/dependencies.props @@ -10,5 +10,6 @@ 6.0.0 5.3.1 3.1.3 + 3.1.3 \ No newline at end of file