2017-09-13 08:47:34 +02:00
|
|
|
|
using BTCPayServer.Configuration;
|
|
|
|
|
using BTCPayServer.Logging;
|
|
|
|
|
using Microsoft.AspNetCore.Hosting;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using System;
|
|
|
|
|
using BTCPayServer.Hosting;
|
|
|
|
|
using NBitcoin;
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Net;
|
2017-09-15 06:49:36 +02:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Collections;
|
2017-09-22 18:31:29 +02:00
|
|
|
|
using Microsoft.AspNetCore.Hosting.Server.Features;
|
|
|
|
|
using System.Threading;
|
2018-10-15 17:37:42 +02:00
|
|
|
|
using Serilog;
|
2017-09-13 08:47:34 +02:00
|
|
|
|
|
|
|
|
|
namespace BTCPayServer
|
|
|
|
|
{
|
2017-10-27 10:53:04 +02:00
|
|
|
|
class Program
|
|
|
|
|
{
|
2018-10-15 17:37:42 +02:00
|
|
|
|
private const long MAX_DEBUG_LOG_FILE_SIZE = 2000000; // If debug log is in use roll it every N MB.
|
|
|
|
|
|
2017-10-27 10:53:04 +02:00
|
|
|
|
static void Main(string[] args)
|
|
|
|
|
{
|
|
|
|
|
ServicePointManager.DefaultConnectionLimit = 100;
|
|
|
|
|
IWebHost host = null;
|
2018-01-15 06:42:51 +01:00
|
|
|
|
var processor = new ConsoleLoggerProcessor();
|
|
|
|
|
CustomConsoleLogProvider loggerProvider = new CustomConsoleLogProvider(processor);
|
2017-10-27 10:53:04 +02:00
|
|
|
|
var loggerFactory = new LoggerFactory();
|
|
|
|
|
loggerFactory.AddProvider(loggerProvider);
|
|
|
|
|
var logger = loggerFactory.CreateLogger("Configuration");
|
|
|
|
|
try
|
|
|
|
|
{
|
2018-10-15 17:37:42 +02:00
|
|
|
|
// This is the only way that LoadArgs can print to console. Because LoadArgs is called by the HostBuilder before Logs.Configure is called
|
2017-10-27 10:53:04 +02:00
|
|
|
|
var conf = new DefaultConfiguration() { Logger = logger }.CreateConfiguration(args);
|
|
|
|
|
if (conf == null)
|
|
|
|
|
return;
|
2018-07-01 08:52:11 +02:00
|
|
|
|
Logs.Configure(loggerFactory);
|
|
|
|
|
new BTCPayServerOptions().LoadArgs(conf);
|
|
|
|
|
Logs.Configure(null);
|
|
|
|
|
/////
|
2017-09-13 08:47:34 +02:00
|
|
|
|
|
2017-10-27 10:53:04 +02:00
|
|
|
|
host = new WebHostBuilder()
|
|
|
|
|
.UseKestrel()
|
|
|
|
|
.UseIISIntegration()
|
|
|
|
|
.UseContentRoot(Directory.GetCurrentDirectory())
|
|
|
|
|
.UseConfiguration(conf)
|
|
|
|
|
.ConfigureLogging(l =>
|
|
|
|
|
{
|
|
|
|
|
l.AddFilter("Microsoft", LogLevel.Error);
|
2019-03-07 11:29:32 +01:00
|
|
|
|
l.AddFilter("System.Net.Http.HttpClient", LogLevel.Critical);
|
2018-01-18 10:12:01 +01:00
|
|
|
|
l.AddFilter("Microsoft.AspNetCore.Antiforgery.Internal", LogLevel.Critical);
|
2018-01-15 06:42:51 +01:00
|
|
|
|
l.AddProvider(new CustomConsoleLogProvider(processor));
|
2018-10-15 17:37:42 +02:00
|
|
|
|
|
|
|
|
|
// Use Serilog for debug log file.
|
2018-11-07 14:29:35 +01:00
|
|
|
|
var debugLogFile = BTCPayServerOptions.GetDebugLog(conf);
|
|
|
|
|
if (string.IsNullOrEmpty(debugLogFile) != false) return;
|
|
|
|
|
Serilog.Log.Logger = new LoggerConfiguration()
|
2018-10-15 17:37:42 +02:00
|
|
|
|
.Enrich.FromLogContext()
|
2018-11-07 14:29:35 +01:00
|
|
|
|
.MinimumLevel.Is(BTCPayServerOptions.GetDebugLogLevel(conf))
|
2018-10-15 17:37:42 +02:00
|
|
|
|
.WriteTo.File(debugLogFile, rollingInterval: RollingInterval.Day, fileSizeLimitBytes: MAX_DEBUG_LOG_FILE_SIZE, rollOnFileSizeLimit: true, retainedFileCountLimit: 1)
|
|
|
|
|
.CreateLogger();
|
|
|
|
|
|
2018-11-07 14:29:35 +01:00
|
|
|
|
l.AddSerilog(Serilog.Log.Logger);
|
2017-10-27 10:53:04 +02:00
|
|
|
|
})
|
|
|
|
|
.UseStartup<Startup>()
|
|
|
|
|
.Build();
|
|
|
|
|
host.StartAsync().GetAwaiter().GetResult();
|
|
|
|
|
var urls = host.ServerFeatures.Get<IServerAddressesFeature>().Addresses;
|
|
|
|
|
foreach (var url in urls)
|
|
|
|
|
{
|
|
|
|
|
logger.LogInformation("Listening on " + url);
|
|
|
|
|
}
|
|
|
|
|
host.WaitForShutdown();
|
|
|
|
|
}
|
|
|
|
|
catch (ConfigException ex)
|
|
|
|
|
{
|
|
|
|
|
if (!string.IsNullOrEmpty(ex.Message))
|
|
|
|
|
Logs.Configuration.LogError(ex.Message);
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
2018-01-15 06:42:51 +01:00
|
|
|
|
processor.Dispose();
|
2018-07-01 08:52:11 +02:00
|
|
|
|
if(host == null)
|
|
|
|
|
Logs.Configuration.LogError("Configuration error");
|
2017-10-27 10:53:04 +02:00
|
|
|
|
if (host != null)
|
|
|
|
|
host.Dispose();
|
2018-10-15 17:37:42 +02:00
|
|
|
|
Serilog.Log.CloseAndFlush();
|
2017-10-27 10:53:04 +02:00
|
|
|
|
loggerProvider.Dispose();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-09-13 08:47:34 +02:00
|
|
|
|
}
|