program config

master
Tudor Stanciu 2020-07-09 02:42:50 +03:00
parent 0e28867c19
commit c889ea4ce7
4 changed files with 78 additions and 13 deletions

View File

@ -18,6 +18,7 @@
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="$(AutoMapperExtensionsPackageVersion)" /> <PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="$(AutoMapperExtensionsPackageVersion)" />
<PackageReference Include="MediatR.Extensions.Microsoft.DependencyInjection" Version="$(MediatRPackageVersion)" /> <PackageReference Include="MediatR.Extensions.Microsoft.DependencyInjection" Version="$(MediatRPackageVersion)" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="$(SwashbucklePackageVersion)" /> <PackageReference Include="Swashbuckle.AspNetCore" Version="$(SwashbucklePackageVersion)" />
<PackageReference Include="Microsoft.Extensions.Hosting.WindowsServices" Version="$(MicrosoftExtensionsPackageVersion)" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View File

@ -1,11 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting; 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 namespace NetworkResurrector.Api
{ {
@ -13,14 +16,70 @@ namespace NetworkResurrector.Api
{ {
public static void Main(string[] args) 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<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", autoCreateSqlTable: true, columnOptions: columnOptions)
.CreateLogger();
try
{
var urls = configuration.GetValue<string>("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) => public static IHostBuilder CreateHostBuilder(string[] args, IConfiguration configuration, bool useWindowsService)
Host.CreateDefaultBuilder(args) {
.ConfigureWebHostDefaults(webBuilder => var builder = Host.CreateDefaultBuilder(args)
{ .ConfigureWebHostDefaults(webBuilder =>
webBuilder.UseStartup<Startup>(); {
}); webBuilder.UseStartup<Startup>()
.UseConfiguration(configuration)
.UseSerilog();
});
if (useWindowsService)
builder.UseWindowsService();
return builder;
}
} }
} }

View File

@ -1,7 +1,11 @@
{ {
"urls": "http://*:5062",
"ConnectionStrings": {
"DatabaseConnection": "***REMOVED***"
},
"Logging": { "Logging": {
"LogLevel": { "LogLevel": {
"Default": "Information", "Default": "Debug",
"Microsoft": "Warning", "Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information" "Microsoft.Hosting.Lifetime": "Information"
} }

View File

@ -10,5 +10,6 @@
<MediatRPackageVersion>6.0.0</MediatRPackageVersion> <MediatRPackageVersion>6.0.0</MediatRPackageVersion>
<SwashbucklePackageVersion>5.3.1</SwashbucklePackageVersion> <SwashbucklePackageVersion>5.3.1</SwashbucklePackageVersion>
<EntityFrameworkCorePackageVersion>3.1.3</EntityFrameworkCorePackageVersion> <EntityFrameworkCorePackageVersion>3.1.3</EntityFrameworkCorePackageVersion>
<MicrosoftExtensionsPackageVersion>3.1.3</MicrosoftExtensionsPackageVersion>
</PropertyGroup> </PropertyGroup>
</Project> </Project>